java设计模式(二)---工厂方法模式

时间:2023-03-09 01:15:50
java设计模式(二)---工厂方法模式

2普通工厂方法模式

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

2.1创建接口

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

2.2创建两个实现

 /**
* 邮件发送
* Created by mrf on 2016/2/25.
*/
public class MailSender implements Sender {
@Override
public String send() {
System.out.println("This is emailSender!");
       return "email";
}
} /**
* 短信发送
* Created by mrf on 2016/2/25.
*/
public class SmsSender implements Sender {
@Override
public String send() {
System.out.println("This is SmsSender!!");return "sms";
}
}

2.3创建工厂

 /**
* 发送工厂
* Created by mrf on 2016/2/25.
*/
public class SendFactory { public Sender produce(String type){
if("email".equals(type)){
return new MailSender();
}
if ("sms".equals(type)){
return new SmsSender();
}
System.out.println("输入类型不正确!");
return null;
}
}

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();
}
}

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();
}
}

2.7总结:

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