• 《java编程思想》:设计模式(不定期更新)


    1.策略设计模式

      创建一个能够根据所传递的参数对象的不同而具有不同的方法,被称为策略设计模式。这类方法包含索要执行的算法中固定不变的部分,而“策略”包含变化的部分。策略就是传递进去的参数对象。在下面的代码示例中,Process对象就是策略。应用在了s上。

    代码示例:

    class Process {
        public String getName(){
            return getClass().getSimpleName();
        }
        Object process(Object input){
            return input;
        }
    }
    
    class Upcase extends Process {
        String process(Object input){
            return ((String)input).toUpperCase();
        }
    }
    
    class Lowercase extends Process {
        String process(Object input){
            return ((String)input).toLowerCase();
        }
    }
    
    public class Strategy{
        public static void process(Process p,Object s){
            System.out.println("Using Process " + p.getName());
            System.out.println(p.process(s));
        }
        public static String s="this is strategy design model!";
    
        public static void main(String[] args) {
            process(new Upcase(),s);
            process(new Lowercase(),s);
        }
    }
    输出:
    Using Process Upcase
    THIS IS STRATEGY DESIGN MODEL!
    Using Process Lowercase
    this is strategy design model!

     2.适配器设计模式

      接收所拥有的接口,产生所需要的接口。

    代码示例:

    public class adapter {
        public static void main(String[] args) {
            new Ademo(new Bdemo()).getBMethod();
        }
    }
    
    class Ademo{
        Bdemo b;
        public Ademo(Bdemo b){
            this.b = b;
        }
        public void getBMethod(){
            b.out();
        }
    }
    
    class Bdemo{
        void out(){
            System.out.println("use b's mothod!");
        }
    }
    输出:
    use b's mothod!

    3.工厂设计模式

      3.1 普通工厂:

       用于生成遵循某个接口的对象。

    代码示例:

    public class factory {
        public static void main(String[] args) {
            Service a = ServiceFactory.getDemoA();
            a.out();
            Service b = ServiceFactory.getDemoB();
            b.out();
        }
    }
    
    interface Service{
        void out();
    }
    
    class demoA implements Service{
    
        @Override
        public void out() {
            System.out.println("i am dome_a !");
        }
    }
    
    class demoB implements Service{
    
        @Override
        public void out() {
            System.out.println("i am dome_b !");
        }
    }
    
    class ServiceFactory{
        public static Service getDemoA(){
            return new demoA();
        }
        public static Service getDemoB(){
            return new demoB();
        }
    }
    输出:
    i am dome_a !
    i am dome_b !

       3.2 使用匿名内部类工厂:

    代码示例:

    public class InnerClassFactory {
        public static void playGame(GameFactory factory){
            Game game = factory.getGame();
            game.play();
        }
    
        public static void main(String[] args) {
            playGame(Football.factory);
            playGame(Basketball.factory);
        }
    }
    
    interface Game{
        void play();
    }
    interface GameFactory{
        Game getGame();
    }
    
    class Football implements Game{
    
        @Override
        public void play() {
            System.out.println("play football game!");
        }
        public static GameFactory factory = new GameFactory(){
    
            @Override
            public Game getGame() {
                return new Football();
            }
        };
    }
    
    class Basketball implements Game{
    
        @Override
        public void play() {
            System.out.println("play basketball game!");
        }
        public static GameFactory factory = new GameFactory(){
    
            @Override
            public Game getGame() {
                return new Basketball();
            }
        };
    }
    输出:
    play football game!
    play basketball game!

      注:相较而言,普通工厂在项目中的使用较多一些,而第二种匿名内部类工厂模式,使用较少。

     
  • 相关阅读:
    Tutorial: Getting Started with TFS in VS2010 (转载)
    Flex开源组件——FlexReport
    GIS和开源见解(摘录)
    Google Wave了解(资料收集)
    分享ArcGis For Flex API 1.3 Diagram
    基于.NET的开源GIS项目收集整理(转载)
    TFS 2010 for SourceSafe Users (转载)
    一个处理UTF8编码文件BOM头的简单方法
    用ISO文件制作启动U盘
    为Ubuntu安装buildessential软件包
  • 原文地址:https://www.cnblogs.com/don9/p/6817924.html
Copyright © 2020-2023  润新知