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总结:

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

posted @   Ryan.Miao  阅读(285)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示