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

    一:工厂方法模式的优点

              --->工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。

                 --->工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中

    二:工厂方法模式的缺点

      --->


    三:应用场景

            --->如果使用简单工厂模式,则工厂类必定过于臃肿。因为简单工厂模式只有一个工厂类,它需要处理所有的创建的逻辑。假如以上需求暂时只支持3种导出的格式以及2种导出的结构,那工厂类则需要6个if else来创建6种不同的类型。如果日后需求不断增加,则后果不堪设想。
      --->这时候就需要工厂方法模式来处理以上需求。在工厂方法模式中,核心的工厂类不再负责所有的对象的创建,而是将具体创建的工作交给子类去做。这个核心类则摇身一变,成为了一个抽象工厂角色,仅负责给出具体工厂子类必须实现的接口,而不接触哪一个类应当被实例化这种细节。

    四:工厂方法模式的角色

            --->抽象工厂(ExportFactory)角色:担任这个角色的是工厂方法模式的核心,任何在模式中创建对象的工厂类必须实现这个接口。在实际的系统中,这个角色也常常使用抽象类实现。

      --->具体工厂(ExportHtmlFactory、ExportPdfFactory)角色:担任这个角色的是实现了抽象工厂接口的具体JAVA类。具体工厂角色含有与业务密切相关的逻辑,并且受到使用者的调用以创建导出类(如:ExportStandardHtmlFile)。

      --->抽象导出(ExportFile)角色:工厂方法模式所创建的对象的超类,也就是所有导出类的共同父类或共同拥有的接口。在实际的系统中,这个角色也常常使用抽象类实现。

      --->具体导出(ExportStandardHtmlFile等)角色:这个角色实现了抽象导出(ExportFile)角色所声明的接口,工厂方法模式所创建的每一个对象都是某个具体导出角色的实例。

    五:工厂方法模式的代码示例
    案例:前台传来不同的信息,生成不同的App工厂类,从而生成不同的App。

    [1]工厂接口

     1 package com.yeepay.sxf.interfaces;
     2 /**
     3  * App的工厂接口
     4  * @author sxf
     5  *
     6  */
     7 public interface APPFactoryInterFace {
     8     /**
     9      * 创建App的方法
    10      * @param info
    11      * @return
    12      */
    13     public App createApp(String info);
    14 }
    View Code

    [2]不同产品的工厂类

     1 package com.yeepay.sxf.interfaces.impl;
     2 
     3 import com.yeepay.sxf.interfaces.APPFactoryInterFace;
     4 import com.yeepay.sxf.interfaces.App;
     5 /**
     6  * 京东商城App工厂
     7  * @author sxf
     8  *
     9  */
    10 public class JdAppFactory  implements APPFactoryInterFace{
    11 
    12     @Override
    13     public App createApp(String info) {
    14         // 创建京东App的逻辑代码
    15         return new JdApp();
    16     }
    17 
    18     
    19 }
    20 
    21 
    22 
    23 package com.yeepay.sxf.interfaces.impl;
    24 
    25 import com.yeepay.sxf.interfaces.APPFactoryInterFace;
    26 import com.yeepay.sxf.interfaces.App;
    27 /**
    28  * 天猫商城App工厂
    29  * @author sxf
    30  *
    31  */
    32 public class TmAppFactory implements APPFactoryInterFace {
    33 
    34     @Override
    35     public App createApp(String info) {
    36         //创建天猫app所使用的业务逻辑代码
    37         return new TmApp();
    38     }
    39     
    40     
    41 
    42 }
    View Code

    [3]产品接口

     1 package com.yeepay.sxf.interfaces;
     2 /**
     3  * 不同app接口
     4  * @author sxf
     5  *
     6  */
     7 public interface App {
     8         /**
     9          * 下订单
    10          * @param orderInfo
    11          * @return
    12          */
    13         public String  giveOrder(String orderInfo);
    14         
    15         /**
    16          * 查询订单功能
    17          * @param orderId
    18          * @return
    19          */
    20         public String  seachOrder(String orderId);
    21 }
    View Code

    [4]不同的产品

     1 package com.yeepay.sxf.interfaces.impl;
     2 
     3 import com.yeepay.sxf.interfaces.App;
     4 /**
     5  * 京东App
     6  * @author sxf
     7  *
     8  */
     9 public class JdApp implements App {
    10 
    11     @Override
    12     public String giveOrder(String orderInfo) {
    13         // TODO Auto-generated method stub
    14         return "向京东下订单:"+orderInfo;
    15     }
    16 
    17     @Override
    18     public String seachOrder(String orderId) {
    19         // TODO Auto-generated method stub
    20         return "查询京东的订单id为:"+orderId;
    21     }
    22 
    23     
    24 }
    25 
    26 
    27 
    28 
    29 package com.yeepay.sxf.interfaces.impl;
    30 
    31 import com.yeepay.sxf.interfaces.App;
    32 /**
    33  * 天猫App
    34  * @author sxf
    35  *
    36  */
    37 public class TmApp implements App {
    38 
    39     @Override
    40     public String giveOrder(String orderInfo) {
    41         // TODO Auto-generated method stub
    42         return "向天猫下订单:"+orderInfo;
    43     }
    44 
    45     @Override
    46     public String seachOrder(String orderId) {
    47         // TODO Auto-generated method stub
    48         return "查询天猫的订单orderId"+orderId;
    49     }
    50     
    51     
    52 }
    View Code

    [4]模拟客户端测试

     1 package com.yeepay.sxf.test;
     2 
     3 import com.yeepay.sxf.interfaces.APPFactoryInterFace;
     4 import com.yeepay.sxf.interfaces.App;
     5 import com.yeepay.sxf.interfaces.impl.JdAppFactory;
     6 import com.yeepay.sxf.interfaces.impl.TmAppFactory;
     7 
     8 public class Test2 {
     9     
    10     public static void main(String[] args) {
    11         //早上10:00:01
    12         APPFactoryInterFace factory=new JdAppFactory();
    13         App app=factory.createApp("创建京东工厂需要的材料");
    14         String result=app.giveOrder("一条牛仔裤");
    15         System.out.println("Test2.main()"+result);
    16         
    17         //早上10:00:01
    18         factory=new TmAppFactory();
    19         app=factory.createApp("创建天猫工厂需要的材料");
    20         result=app.giveOrder("一条花裙子");
    21         System.out.println("Test2.main()"+result);
    22         
    23     }
    24 
    25     
    26 }
    View Code

    [5]测试结果

    Test2.main()向京东下订单:一条牛仔裤
    Test2.main()向天猫下订单:一条花裙子

    ============简单工厂模式=================

     ============工厂模式=================

     ============抽象工厂模式=================

  • 相关阅读:
    排列专题(不定期更新)
    搜索专题(不定期更新)
    Redis 高级面试题
    面试题1
    CentOS7查看开放端口命令及开放端口号
    Union和Union All到底有什么区别
    浅谈MySQL中优化sql语句查询常用的30种方法
    什么是分布式系统,如何学习分布式系统(转)
    浅析分布式系统(转)
    什么是分布式系统(通俗易懂的说法)(转)
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/4640984.html
Copyright © 2011-2022 走看看