简单工厂模式
普通做法:
package com.zhen.build_template.simple_factory.general; /** * @author zhen * @Date 2019/5/28 11:13 */ public interface Sender { public void send(); } package com.zhen.build_template.simple_factory.general; /** * @author zhen * @Date 2019/5/28 11:13 */ public class MailSender implements Sender{ @Override public void send() { System.out.println("this is mailSender"); } } package com.zhen.build_template.simple_factory.general; /** * @author zhen * @Date 2019/5/28 11:14 */ public class SmsSender implements Sender { @Override public void send() { System.out.println("this is SmsSender"); } } package com.zhen.build_template.simple_factory.general; /** * @author zhen * @Date 2019/5/28 11:15 */ public class SendFactory { public Sender getSenderInstance(String type){ if ("mail".equals(type)){ return new MailSender(); }else if("sms".equals(type)){ return new SmsSender(); }else{ System.out.println("请输入正确的类型"); return null; } } } package com.zhen.build_template.simple_factory.general; /** * @author zhen * @Date 2019/5/28 11:17 */ public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.getSenderInstance("sms"); sender.send(); } }
普通简单工厂有一个问题:
如果传入的类型是错误的,则得不到对象
多方法类型做法:
package com.zhen.build_template.simple_factory.many_method; /** * @author zhen * @Date 2019/5/28 11:15 */ public class SendFactory { public Sender getSmsSenderInstance(){ return new SmsSender(); } public Sender getMailSenderInstance(){ return new MailSender(); } } package com.zhen.build_template.simple_factory.many_method; /** * @author zhen * @Date 2019/5/28 11:17 */ public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.getMailSenderInstance(); sender.send(); } } package com.zhen.build_template.simple_factory.many_method; /** * @author zhen * @Date 2019/5/28 11:13 */ public interface Sender { public void send(); } package com.zhen.build_template.simple_factory.many_method; /** * @author zhen * @Date 2019/5/28 11:13 */ public class MailSender implements Sender { @Override public void send() { System.out.println("this is mailSender"); } } package com.zhen.build_template.simple_factory.many_method; /** * @author zhen * @Date 2019/5/28 11:14 */ public class SmsSender implements Sender { @Override public void send() { System.out.println("this is SmsSender"); } }
使用多个方法解决了传入错误类型得不到对象的情况,但是每次创造对象都依赖工厂实例
多静态方法做法:
package com.zhen.build_template.simple_factory.many_static_method; /** * @author zhen * @Date 2019/5/28 11:13 */ public interface Sender { public void send(); } package com.zhen.build_template.simple_factory.many_static_method; /** * @author zhen * @Date 2019/5/28 11:13 */ public class MailSender implements Sender { @Override public void send() { System.out.println("this is mailSender"); } } package com.zhen.build_template.simple_factory.many_static_method; /** * @author zhen * @Date 2019/5/28 11:14 */ public class SmsSender implements Sender { @Override public void send() { System.out.println("this is SmsSender"); } } package com.zhen.build_template.simple_factory.many_static_method; /** * @author zhen * @Date 2019/5/28 11:15 */ public class SendFactory { public static Sender getSmsSenderInstance(){ return new SmsSender(); } public static Sender getMailSenderInstance(){ return new MailSender(); } } package com.zhen.build_template.simple_factory.many_static_method; /** * @author zhen * @Date 2019/5/28 11:17 */ public class FactoryTest { public static void main(String[] args) { Sender sender = SendFactory.getMailSenderInstance(); sender.send(); } }
使用的静态方法方式解决了依赖工厂实例的问题,一般简单工厂都采用第三种。