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

    2普通工厂方法模式

    就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。

    2.1创建接口

    1 /**
    2  * 发送接口
    3  * Created by mrf on 2016/2/25.
    4  */
    5 public interface Sender {
    6     public String send();
    7 }

    2.2创建两个实现

     1 /**
     2  * 邮件发送
     3  * Created by mrf on 2016/2/25.
     4  */
     5 public class MailSender implements Sender {
     6     @Override
     7     public String send() {
     8         System.out.println("This is emailSender!");
           return "email";
    9 } 10 } 11 12 /** 13 * 短信发送 14 * Created by mrf on 2016/2/25. 15 */ 16 public class SmsSender implements Sender { 17 @Override 18 public String send() { 19 System.out.println("This is SmsSender!!");return "sms"; 20 } 21 }

    2.3创建工厂

     1 /**
     2  * 发送工厂
     3  * Created by mrf on 2016/2/25.
     4  */
     5 public class SendFactory {
     6 
     7     public Sender produce(String type){
     8         if("email".equals(type)){
     9             return new MailSender();
    10         }
    11         if ("sms".equals(type)){
    12             return new SmsSender();
    13         }
    14         System.out.println("输入类型不正确!");
    15         return null;
    16     }
    17 }

    2.4使用测试

    /** * Created by mrf on 2016/2/25 */
    public
    class SendFactoryTest { protected long startTime; protected long endTime; @Before public void setUp() throws Exception { this.startTime= System.currentTimeMillis(); System.out.println("=========开始测试==========="); } @After public void tearDown() throws Exception { this.endTime = System.currentTimeMillis(); System.out.println("测试用时:"+(endTime-startTime)); System.out.println("=========测试结束==========="); } @Test public void testProduce() throws Exception { SendFactory sendFactory = new SendFactory(); Sender sender = sendFactory.produce("email");
    String send = sender.send();
    assertEquals("email",send);
    } }

     2.5多个工厂方法模式

    package com.test.java.designPattern.factory;
    
    /**
     * 多个工厂模式
     * <p>
     *     是对普通工厂方法模式的改进。因为普通方法模式中key错误则不能正确创建对象,
     *     多个工厂模式提供多个创建方法。
     * </p>
     * Created by mrf on 2016/2/26.
     */
    public class MultiSendFacoty {
    
        public Sender produceMail(){
            return  new MailSender();
        }
    
        public Sender produceSms(){
            return  new SmsSender();
        }
    }

    测试:

    package com.test.java.designPattern.factory;
    
    import org.junit.Before;
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    
    /**
     * Created by mrf on 2016/2/26.
     */
    public class MultiSendFacotyTest {
        private MultiSendFacoty facoty;
    
        @Before
        public void setUp(){
            facoty = new MultiSendFacoty();
        }
    
        @Test
        public void testProduceMail() throws Exception {
            Sender sender = facoty.produceMail();
            sender.send();
    
        }
    
        @Test
        public void testProduceSms() throws Exception {
            Sender sender = facoty.produceSms();
            sender.send();
        }
    }
    View Code

     2.6静态工厂方法模式

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

    package com.test.java.designPattern.factory;
    
    import org.junit.Before;
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    
    /**
     * Created by mrf on 2016/2/26.
     */
    public class StaticSendFacotyTest {
    
        @Test
        public void testProduceMail() throws Exception {
            Sender sender = StaticSendFacoty.produceMail();
            sender.send();
        }
    
        @Test
        public void testProduceSms() throws Exception {
            Sender sender = StaticSendFacoty.produceSms();
            sender.send();
        }
    }
    View Code

    2.7总结:

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

  • 相关阅读:
    [Winform]Media Player com组件应用中遇到的问题
    [Winform]Media Player播放控制面板控制,单击事件截获
    [Winform]Media Player组件全屏播放的设置
    [EF]数据上下文该如何实例化?
    [Winform]在关闭程序后,托盘不会消失的问题
    咏南中间件跨平台解决方案
    硬件中间件
    BASE64使用场景
    DELPHI7 ADO二层升三层新增LINUX服务器方案
    NGINX心跳检测
  • 原文地址:https://www.cnblogs.com/woshimrf/p/5217742.html
Copyright © 2011-2022 走看看