• 设计模式之美—简单工厂模式


    简单工厂模式

      简单工厂模式分为三种:普通简单工厂、多方法简单工厂、静态方法简单工厂。

    普通工厂模式

      最近看了老酒馆电视剧,深深被陈怀海他们的情怀所感动,当然里面也有很多的酒,比如扳倒井,闷倒驴,跑舌头,吹破天,二闺女,枕头亲。我们以酒为例:

      创建酒的接口:

    public interface Liqueur {
        public void taste();//酒味
    }

      创建实现类:

      (1)闷倒驴味道

    1 public class Mdl implements Liqueur {
    2     @Override
    3     public void taste() {
    4         System.out.println("我是闷倒驴,辣的!");
    5     }
    6 }

       

      (2)跑舌头味道(里面的杜先生舌头惹了祸,没了,特意点了这跑舌头)

    1 public class Pst implements Liqueur {
    2     @Override
    3     public void taste() {
    4         System.out.println("我是跑舌头,苦的!");
    5     }
    6 }

       

      建工厂类:

     1 public class MakeLiqueurFactory {
     2 
     3     /**
     4      * 制造闷倒驴和跑舌头
     5      */
     6     public Liqueur make(String type){
     7         if ("mdl".equalsIgnoreCase(type)){
     8             return new Mdl();
     9         }else if ("pst".equalsIgnoreCase(type)){
    10             return new Pst();
    11         }else {
    12             return null;
    13         }
    14     }
    15 }

       

      测试:

     1 public class LiqueurTest {
     2 
     3     public static void main(String[] args){
     4         MakeLiqueurFactory factory = new MakeLiqueurFactory();
     5         Liqueur mdl = factory.make("mdl");
     6         mdl.taste();
     7         Liqueur pst = factory.make("pst");
     8         pst.taste();
     9     }
    10 }

    多方法简单工厂

      在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。

     1 public class MakeLiqueurFactory {
     2 
     3     /**
     4      * 制造闷倒驴
     5      */
     6     public Liqueur makeMdl(){
     7         return new Mdl();
     8     }
     9 
    10     /**
    11      * 制造跑舌头
    12      */
    13     public Liqueur makePst(){
    14         return new Pst();
    15     }
    16 }

       

      测试:

     1 public class LiqueurTest {
     2 
     3     public static void main(String[] args){
     4         MakeLiqueurFactory factory = new MakeLiqueurFactory();
     5         Liqueur mdl = factory.makeMdl();
     6         mdl.taste();
     7         Liqueur pst = factory.makeMdl();
     8         pst.taste();
     9     }
    10 }

    静态方法简单工厂

      将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。

     1 public class MakeLiqueurFactory {
     2 
     3     /**
     4      * 制造闷倒驴
     5      */
     6     public static Liqueur makeMdl(){
     7         return new Mdl();
     8     }
     9 
    10     /**
    11      * 制造跑舌头
    12      */
    13     public static Liqueur makePst(){
    14         return new Pst();
    15     }
    16 }

      

      测试:

    1 public class LiqueurTest {
    2 
    3     public static void main(String[] args){
    4         Liqueur mdl = MakeLiqueurFactory.makeMdl();
    5         mdl.taste();
    6         Liqueur pst = MakeLiqueurFactory.makePst();
    7         pst.taste();
    8     }
    9 }

      

      结果都是如下所示:  

    1 我是闷倒驴,辣的!
    2 我是跑舌头,苦的!

    在以上的三种模式中,第一种如果传入的字符串有误,不能正确创建对象,第三种相对于第二种,不需要实例化工厂类,所以,大多数情况下,我们会选用第三种——静态工厂方法模式。

  • 相关阅读:
    集合中的3个经典练习题
    创建泛类集合List以及数组转集合,集合转数组的应用
    添加一个txt文件(例如在桌面),利用后台对文件写入内容
    File类的创建,删除文件
    集合中存放随机数的问题之解析
    集合中的类型转化 以及求集合中元素的最大值,平均值
    response.sendRedirect()使用注意事项
    request.getContextPath是为了解决相对路径的问题,可返回站点的根路径
    java中instanceof的用法
    getParameter 与 getAttribute的区别
  • 原文地址:https://www.cnblogs.com/yeshensi/p/11676371.html
Copyright © 2020-2023  润新知