一、简单工厂
package design.pattern.factory; public class SimpleFactory { public static void main(String[] args) { SimpleFactory factory = new SimpleFactory(); System.out.println(factory.produce(EmailMaker.class, "Lucy")); System.out.println(factory.produce(CallMaker.class, "Neo")); } /** 指定类型, 根据入参生成产品*/ public Product produce(Class<?> cls, String param) { if (cls.isAssignableFrom(EmailMaker.class)) { return new EmailMaker().make(param); } else if (cls.isAssignableFrom(CallMaker.class)) { return new CallMaker().make(param); } return null; } /**-----------------------------------------------------------*/ static class Product { } static class Email extends Product { @Override public String toString() { return " this an Email"; } } static class Call extends Product { @Override public String toString() { return " this a Call"; } } /**-----------------------------------------------------------*/ interface Maker { public Product make(String type); } class EmailMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Email(); } } class CallMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Call(); } } }
二、工厂方法
package design.pattern.factory; public class FactoryMethod { public static void main(String[] args) { FactoryMethod factory=new FactoryMethod(); System.out.println(factory.produceEmail("Lucy")); System.out.println(factory.produceCall("Neo")); } /**每个产品一个方法*/ public Call produceCall(String param) { return (Call) new CallMaker().make(param); } public Email produceEmail(String param) { return (Email) new EmailMaker().make(param); } /**-----------------------------------------------------------*/ static class Product { } static class Email extends Product { @Override public String toString() { return " this an Email"; } } static class Call extends Product { @Override public String toString() { return " this a Call"; } } /**-----------------------------------------------------------*/ interface Maker { public Product make(String type); } class EmailMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Email(); } } class CallMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Call(); } } }
2.1 静态工厂:
因为是内部类, 所以需要先 new FactoryMethod()
public static Call produceCall(String param) { return (Call) new FactoryMethod().new CallMaker().make(param); }
三、抽象工厂
package design.pattern.factory; public class AbstractFactorys { public static void main(String[] args) { AbstractFactorys factorys = new AbstractFactorys(); System.out.println(factorys.new EmailFactory().work().make("Lucy")); System.out.println(factorys.new CallFactory().work().make("Neo")); } /**定制工厂,每个工厂只能生产指定产品*/ /**-----------------------------------------------------------*/ interface Factory{ Maker work(); } class EmailFactory implements Factory{ @Override public Maker work() { return new EmailMaker(); } } class CallFactory implements Factory{ @Override public Maker work() { return new CallMaker(); } } /**-----------------------------------------------------------*/ static class Product { } static class Email extends Product { @Override public String toString() { return " this an Email"; } } static class Call extends Product { @Override public String toString() { return " this a Call"; } } /**-----------------------------------------------------------*/ interface Maker { public Product make(String type); } class EmailMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Email(); } } class CallMaker implements Maker { @Override public Product make(String param) { System.out.println("Hi, " + param); return new Call(); } } }
四、范型工厂
可以统一构造相同属性。
package design.pattern.factory; public class GenericFactory { public static void main(String[] args) { MobileRQ rq = GenericFactory.createRQ(MobileRQ.class); System.out.println(rq); } public static <T extends RQ> T createRQ(Class<T> cls) { try { T rq = cls.newInstance(); rq.setAccessTocken("a341p28y^&s"); rq.setTimeStamp(String.valueOf(System.currentTimeMillis())); return rq; } catch (Exception e) { e.printStackTrace(); } return null; } static class RQ { private String accessTocken; private String timeStamp; public String getAccessTocken() { return accessTocken; } public void setAccessTocken(String accessTocken) { this.accessTocken = accessTocken; } public String getTimeStamp() { return timeStamp; } public void setTimeStamp(String timeStamp) { this.timeStamp = timeStamp; } @Override public String toString() { return "RQ [accessTocken=" + accessTocken + ", timeStamp=" + timeStamp + "]"; } } static class PCRQ extends RQ { /* something else */ } static class MobileRQ extends RQ { /* something else */ } }