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

    一、什么是工厂方法模式


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

     

    二、模式中包含的角色及其职责


    1.抽象工厂(Creator)角色 工厂方法模式的核心,任何工厂类都必须实现这个接口。
    2.具体工厂( Concrete  Creator)角色 具体工厂类是抽象工厂的一个实现,负责实例化产品对象。
    3.抽象(Product)角色 工厂方法模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。
    4.具体产品(Concrete Product)角色 工厂方法模式所创建的具体实例对象

     

    三、工厂方法模式和简单工厂模式比较


      工厂方法模式与简单工厂模式在结构上的不同不是很明显。工厂方
    法类的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具
    体类上。
        工厂方法模式之所以有一个别名叫多态性工厂模式是因为具体工
    厂类都有共同的接口,或者有共同的抽象父类。当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对
    象以及一个具体工厂对象,原有工厂对象不需要进行任何修改,也
    不需要修改客户端,很好的符合了“开放-封闭”原则。而简单工厂
    模式在添加新产品对象后不得不修改工厂方法,扩展性不好。工厂方法模式退化后可以演变成简单工厂模式。


    以下是实例:

     1 public class MainClass {
     2     public static void main(String[] args) {
     3         //获得AppleFactory
     4         FruitFactory ff = new AppleFactory();
     5         //通过AppleFactory来获得Apple实例对象
     6         Fruit apple = ff.getFruit();
     7         apple.get();
     8         
     9         //获得BananaFactory
    10         FruitFactory ff2 = new BananaFactory();
    11         Fruit banana = ff2.getFruit();
    12         banana.get();
    13         
    14         //获得PearFactory
    15         FruitFactory ff3 = new PearFactory();
    16         Fruit pear = ff3.getFruit();
    17         pear.get();
    18     }
    19 }

    抽象工厂

    1 public interface FruitFactory {
    2     public Fruit getFruit();
    3 }

    具体工厂

    1 public class AppleFactory implements FruitFactory {
    2 
    3     public Fruit getFruit() {
    4         return new Apple();
    5     }
    6 
    7 }
    1 public class BananaFactory implements FruitFactory {
    2 
    3     public Fruit getFruit() {
    4         return new Banana();
    5     }
    6 
    7 }
    1 public class PearFactory implements FruitFactory {
    2 
    3     public Fruit getFruit() {
    4         return new Pear();
    5     }
    6 }

    抽象

    1 public interface Fruit {
    2     /*
    3      * 采集
    4      */
    5     public void get();
    6 }

    具体产品

    1 public class Apple implements Fruit{
    2     /*
    3      * 采集
    4      */
    5     public void get(){
    6         System.out.println("采集苹果");
    7     }
    8 }
    1 public class Banana implements Fruit{
    2     /*
    3      * 采集
    4      */
    5     public void get(){
    6         System.out.println("采集香蕉");
    7     }
    8 }
    1 public class Pear implements Fruit {
    2 
    3     public void get() {
    4         System.out.println("采集梨子");
    5     }
    6 
    7 }

     

  • 相关阅读:
    C# DataGridView 与 datatable 之间数据传递
    C# 调用命令行命令 net use
    C# 链接 sql server 数据库字符串
    winform窗口关闭,进程没有关掉的解决办法
    select 中的逻辑判断 sql server
    C#中的abstract 类和方法!!!
    c# ComboBox绑定枚举
    C#与C++类型互转
    DllImport
    TCP三次握手四次挥手详解
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/13786271.html
Copyright © 2011-2022 走看看