zoukankan      html  css  js  c++  java
  • Java设计模式系列2--工厂方法模式(Factory Method)

    2014-02-26 09:56:45

    声明:本文不仅是本人自己的成果,有些东西取自网上各位大神的思想,虽不能一一列出,但在此一并感谢!

    工厂方法模式分为三种:

    1. 普通工厂模式

    建立一个工厂类,对实现了同一接口的一些类进行实例的创建,如下图:

    代码示例如下:

     1 public interface Sender {
     2     public void send();
     3 }
     4 
     5 class MailSender implements Sender {
     6 
     7     @Override
     8     public void send() {
     9         System.out.println("This is MailSender!");
    10     }
    11 }
    12 
    13 class SmsSender implements Sender {
    14 
    15     @Override
    16     public void send() {
    17         System.out.println("This is SmsSender!");
    18     }
    19 }
    20 
    21 class SendFactory {
    22     public Sender produce(String type) {
    23         if ("mail".equals(type)) {
    24             return new MailSender();
    25         } else if ("sms".equals(type)) {
    26             return new SmsSender();
    27         } else {
    28             System.out.println("请输入正确的类型!");
    29             return null;
    30         }
    31     }
    32 }

    2. 多个工厂方法模式

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

    代码示例如下:

     1 public interface Sender {
     2     public void send();
     3 }
     4 
     5 class MailSender implements Sender {
     6 
     7     @Override
     8     public void send() {
     9         System.out.println("This is MailSender!");
    10     }
    11 }
    12 
    13 class SmsSender implements Sender {
    14 
    15     @Override
    16     public void send() {
    17         System.out.println("This is SmsSender!");
    18     }
    19 }
    20 
    21 class SendFactory {
    22 
    23     public Sender produceMail() {
    24         return new MailSender();
    25     }
    26 
    27     public Sender produceSms() {
    28         return new SmsSender();
    29     }
    30 }

    3. 静态工厂方法模式

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

    代码示例如下:

     1 public interface Sender {
     2     public void send();
     3 }
     4 
     5 class MailSender implements Sender {
     6 
     7     @Override
     8     public void send() {
     9         System.out.println("This is MailSender!");
    10     }
    11 }
    12 
    13 class SmsSender implements Sender {
    14 
    15     @Override
    16     public void send() {
    17         System.out.println("This is SmsSender!");
    18     }
    19 }
    20 
    21 class SendFactory {
    22 
    23     public static Sender produceMail() {
    24         return new MailSender();
    25     }
    26 
    27     public static Sender produceSms() {
    28         return new SmsSender();
    29     }
    30 }

    总体来说,凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建。在以上的三种模式中,第一种如果传入的字符串有误,不能正确创建对象,第三种相对于第二种,不需要实例化工厂类,所以,大多数情况下,我们会选用第三种——静态工厂方法模式。

  • 相关阅读:
    java基础部分的一些有意思的东西。
    antdvue按需加载插件babelpluginimport报错
    阿超的烦恼 javaScript篇
    .NET E F(Entity Framework)框架 DataBase First 和 Code First 简单用法。
    JQuery获得input ID相同但是type不同的方法
    gridview的删除,修改,数据绑定处理
    jgGrid数据格式
    Cannot read configuration file due to insufficient permissions
    Invoke action which type of result is JsonResult on controller from view using Ajax or geJSon
    Entity model数据库连接
  • 原文地址:https://www.cnblogs.com/wlrhnh/p/3568509.html
Copyright © 2011-2022 走看看