1.普通工厂模式
工厂类
/** * @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 上午10:16:02 * @version V1.0 */package factory.factory1;/** * @ClassName Factory * @date 2015-1-22 上午10:16:02 */public class Factory { public Sender procedure(String type) { if("mail".endsWith(type)) { return new MailSender(); } if("sms".endsWith(type)) { return new SMSSender(); }else { System.out.println("未输入正确的类型"); //更好的话加入异常 return null; } }}
发送器接口
/** * @Title Sender.java * @Package factory.factory1 * @date 2015-1-22 上午10:07:24 * @version V1.0 */package factory.factory1;/** * @ClassName Sender * @date 2015-1-22 上午10:07:24 */public interface Sender {public void send();}
发送器实现类
/** * @Title MailSender.java * @Package factory.factory1 * @date 2015-1-22 上午10:09:29 * @version V1.0 */package factory.factory1;/** * @ClassName MailSender * @date 2015-1-22 上午10:09:29 */public class MailSender implements Sender{ /* (non-Javadoc) * @see factory.factory1.Sender#send() */ @Override public void send() { System.out.println("this is a mailsender"); }}
/** * @Title SMSSender.java * @Package factory.factory1 * @date 2015-1-22 上午10:14:36 * @version V1.0 */package factory.factory1;/** * @ClassName SMSSender * @date 2015-1-22 上午10:14:36 */public class SMSSender implements Sender{ /* (non-Javadoc) * @see factory.factory1.Sender#send() */ @Override public void send() { System.out.println("this is a smssender"); }}
测试类:
/** * @Title Test.java * @Package factory.factory1 * @date 2015-1-22 上午10:20:19 * @version V1.0 */package factory.factory1;/** * @ClassName Test * @date 2015-1-22 上午10:20:19 */public class Test {public static void main(String[] args) { Factory factory = new Factory(); Sender sender = factory.procedure("mail"); sender.send();}}
这种方法的实现方式对传入的类型要求比较高,就是传入的参数如果错误就不能够获得相对应的产生的东西。
2.多个工厂的方法
/** * @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 上午10:16:02 * @version V1.0 */package factory.factory3;/** * @ClassName Factory * @date 2015-1-22 上午10:16:02 */public class Factory { public Sender mailSender(){ return new MailSender(); } public Sender smsSender(){ return new SMSSender(); }}
将上面的工厂类修改成这样子即可。
测试类:
/** * @Title Test.java * @Package factory.factory1 * @date 2015-1-22 上午10:20:19 * @version V1.0 */package factory.factory3;/** * @ClassName Test * @date 2015-1-22 上午10:20:19 */public class Test {public static void main(String[] args) { Factory factory = new Factory(); Sender sender = factory.mailSender(); sender.send();}}
3.静态工厂的方法 将工厂的方法修改成静态的方法
/** * @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 上午10:16:02 * @version V1.0 */package factory.factory4;/** * @ClassName Factory * @date 2015-1-22 上午10:16:02 */public class Factory { public static Sender mailSender(){ return new MailSender(); } public static Sender smsSender(){ return new SMSSender(); }}
测试类
/** * @Title Test.java * @Package factory.factory1 * @date 2015-1-22 上午10:20:19 * @version V1.0 */package factory.factory4;/** * @ClassName Test * @date 2015-1-22 上午10:20:19 */public class Test {public static void main(String[] args) {// Factory factory = new Factory(); Sender sender = Factory.mailSender(); sender.send();}}
4.抽象工厂模式
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定 的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。因为抽象工厂不 太好理解,我们先看看图,然后就和代码,就比较容易理解。
提供一个工厂的接口
/** * @Title Provider.java * @Package factory.factory2 * @date 2015-1-22 上午10:36:18 * @version V1.0 */package factory.factory2;/** * @ClassName Provider * @date 2015-1-22 上午10:36:18 */public interface Provider {public Sender procedure();}
提供一个发送的接口
/** * @Title Sender.java * @Package factory.factory2 * @date 2015-1-22 上午10:37:03 * @version V1.0 */package factory.factory2;/** * @ClassName Sender * @date 2015-1-22 上午10:37:03 */public interface Sender {public void send();}
分别两个类来实现这发送接口
/** * @Title MailSender.java * @Package factory.factory2 * @date 2015-1-22 上午10:45:05 * @version V1.0 */package factory.factory2;/** * @ClassName MailSender * @date 2015-1-22 上午10:45:05 */public class MailSender implements Sender{ /* (non-Javadoc) * @see factory.factory2.Sender#send() */ @Override public void send() { System.out.println("this is a mail sender"); }}
/** * @Title SmsSender.java * @Package factory.factory2 * @date 2015-1-22 上午10:40:06 * @version V1.0 */package factory.factory2;/** * @ClassName SmsSender * @date 2015-1-22 上午10:40:06 */public class SmsSender implements Sender{ /* (non-Javadoc) * @see factory.factory2.Sender#send() */ @Override public void send() { System.out.println("this is a mail sender"); }}
提供两个类实现抽象工厂的接口
/** * @Title SendMailFactory.java * @Package factory.factory2 * @date 2015-1-22 上午10:44:34 * @version V1.0 */package factory.factory2;/** * @ClassName SendMailFactory * @date 2015-1-22 上午10:44:34 */public class SendMailFactory implements Provider { /* (non-Javadoc) * @see factory.factory2.Provider#procedure() */ @Override public Sender procedure() { // TODO Auto-generated method stub return new MailSender(); }}
/** * @Title SmsSendFactory.java * @Package factory.factory2 * @date 2015-1-22 上午10:41:23 * @version V1.0 */package factory.factory2;/** * @ClassName SmsSendFactory * @date 2015-1-22 上午10:41:23 */public class SmsSendFactory implements Provider{ /* (non-Javadoc) * @see factory.factory2.Provider#procedure() */ @Override public Sender procedure() { // TODO Auto-generated method stub return new SmsSender(); }}
测试类:
/** * @Title Test.java * @Package factory.factory2 * @date 2015-1-22 上午10:48:29 * @version V1.0 */package factory.factory2;/** * @ClassName Test * @date 2015-1-22 上午10:48:29 */public class Test {public static void main(String[] args) { Provider provider = new SendMailFactory(); Sender sender = provider.procedure(); sender.send();}}