zoukankan      html  css  js  c++  java
  • 设计模式之工厂方法模式

    1.什么是工厂方法模式

      工厂方法模式同样属于类的创建型模式又被称为多态工厂模式 。工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,
    这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。

    2.工厂方法模式与简单工厂模式比较

      工厂方法模式与简单工厂模式在结构上的不同不是很明显。工厂方法类的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。 
      工厂方法模式之所以有一个别名叫多态性工厂模式是因为具体工厂类都有共同的接口,或者有共同的抽象父类。当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对
    象以及一个具体工厂对象,原有工厂对象不需要进行任何修改,也不需要修改客户端,很好的符合了“开放-封闭”原则。而简单工厂模式在添加新产品对象后不得不修改工厂方法,
    扩展性不好。 工厂方法模式退化后可以演变成简单工厂模式。
    public interface Fruit {
        /*
         * 采集
         */
        public void get();
    }
    public class Apple implements Fruit{
        /*
         * 采集
         */
        public void get(){
            System.out.println("采集苹果");
        }
    }
    public class Banana implements Fruit{
        /*
         * 采集
         */
        public void get(){
            System.out.println("采集香蕉");
        }
    }
    public class Pear implements Fruit {
    
        public void get() {
            System.out.println("采集梨子");
        }
    
    }
    public interface FruitFactory {
        public Fruit getFruit();
    }
    public class AppleFactory implements FruitFactory {
    
        public Fruit getFruit() {
            return new Apple();
        }
    
    }
    public class BananaFactory implements FruitFactory {
    
        public Fruit getFruit() {
            return new Banana();
        }
    
    }
    public class PearFactory implements FruitFactory {
    
        public Fruit getFruit() {
            return new Pear();
        }
    }
    public class MainClass {
        public static void main(String[] args) {
            //获得AppleFactory
            FruitFactory ff = new AppleFactory();
            //通过AppleFactory来获得Apple实例对象
            Fruit apple = ff.getFruit();
            apple.get();
            
            //获得BananaFactory
            FruitFactory ff2 = new BananaFactory();
            Fruit banana = ff2.getFruit();
            banana.get();
            
            //获得PearFactory
            FruitFactory ff3 = new PearFactory();
            Fruit pear = ff3.getFruit();
            pear.get();
        }
    }
  • 相关阅读:
    Windows10如何添加开机启动项
    selenium自动化文件上传、文件下载
    windows下创建虚拟环境
    selenium 处理js日历控件
    selenium鼠标和键盘事件
    selenium绕过某宝验证,手动验证滑块
    python国内镜像源
    Linux下jenking安装
    发送邮件
    基于python的selenium两种文件上传操作
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/10859252.html
Copyright © 2011-2022 走看看