工厂模式就是好
工厂模式很普遍,想必好多人一开始接触的就是这个模式了
工厂方法模式一种创建对象的模式,它被广泛应用在jdk中以及Spring、Mybatis、Hibernate和Struts框架中;
工厂方法模式基于"输入",应用在超类和多个子类之间的情况,这种模式将创建对象的责任转移到工厂类;
首先让我们学习一下如何在Java中应用工厂方法模式并且学习到工厂方法的优点,另外工厂方法模式也广泛应用在jdk中;
超类可以是接口、抽象类、父类,本例中将通过重写 toString() 方法来解释工厂方法模式;
结构图如下:
基类代码:
/** * * @author dgm * @describe "" * @date 2020年5月12日 */ public abstract class Computer { public abstract String getRAM(); public abstract String getHDD(); public abstract String getCPU(); @Override public String toString(){ return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU(); } }
工厂模式子类,有四种实现:
public class PC extends Computer { private String ram; private String hdd; private String cpu; public PC(String ram, String hdd, String cpu){ this.ram=ram; this.hdd=hdd; this.cpu=cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } } public class Pad extends Computer { private String ram; private String hdd; private String cpu; public Pad(String ram, String hdd, String cpu){ this.ram=ram; this.hdd=hdd; this.cpu=cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } } public class MobilePhone extends Computer { private String ram; private String hdd; private String cpu; public MobilePhone(String ram, String hdd, String cpu){ this.ram=ram; this.hdd=hdd; this.cpu=cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } } public class Server extends Computer { private String ram; private String hdd; private String cpu; public Server(String ram, String hdd, String cpu){ this.ram=ram; this.hdd=hdd; this.cpu=cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } }
定义工厂类
现在有了多个子类和超类,接下来可以创建工厂类了:
/** * * @author dgm * @describe "产品类型" * @date 2020年5月12日 */ public enum ComputerType { PC, SERVER, PAD, MOBILEPHONE } /** * * @author dgm * @describe "生产产品的工厂,类似富士康" * @date 2020年5月12日 */ public class ComputerFactory { public static Computer getComputer(ComputerType type, String ram, String hdd, String cpu){ Computer product = null; switch (type) { case PC: product = new PC(ram, hdd, cpu); break; case SERVER: product = new Server(ram, hdd, cpu); break; case MOBILEPHONE: product = new MobilePhone(ram, hdd, cpu); break; case PAD: product = new Pad(ram, hdd, cpu); break; } return product; } }
需要重点指出的是:
工厂类可以是单例的,getComputer 可以是静态的;
getComputer 是工厂类的方法,且基于相同的参数类型返回了不同的对象;
接下来是一个简单的测试客户端程序,它使用上面的工厂设计模式实现。
/** * * @author dgm * @describe "测试工厂生产的产品合格吗" * @date 2020年5月12日 */ public class TestFactory { public static void main(String[] args) { Computer pc = ComputerFactory.getComputer(ComputerType.PC,"2 GB","500 GB","2.4 GHz"); Computer server = ComputerFactory.getComputer(ComputerType.SERVER,"16 GB","1 TB","2.9 GHz"); Computer pad = ComputerFactory.getComputer(ComputerType.PAD,"8 GB","320 GB","3.0 GHz"); Computer phone = ComputerFactory.getComputer(ComputerType.MOBILEPHONE,"8 GB","128 GB","2.8 GHz"); System.out.println("Factory PC Config::"+pc); System.out.println("Factory Server Config::"+server); System.out.println("Factory pad Config::"+pad); System.out.println("Factory mobilePhone Config::"+phone); } }
输出结果:
很好四款产品都合格,没有劣质产品
小结: 工厂模式很好用,也很实用!!!师傅领进门,修行靠个人,不要老背代码(看到不少人喜欢背代码却不会变通),要想不同的模式在场景怎么使用、如何使用、使用哪种模式,不是靠死记硬背,当然了也许他们搞明白概念,加油!!!
附代码已上传 https://github.com/dongguangming/design-pattern/tree/master/src/code/factory
参考:
1. Java Design Pattern: Factory https://www.programcreek.com/2013/02/java-design-pattern-factory/
2、Implement factory pattern in Spring when using Java configuration.
https://javajee.com/content/recipe-factory-beans-in-spring-with-java-configuration
https://www.journaldev.com/1392/factory-design-pattern-in-java
4. Factory Design Pattern in Java (要翻墙)http://adnjavainterview.blogspot.com/2015/07/factory-design-pattern-in-java.html?m=1
5. 要翻墙 https://www.javaguides.net/2018/07/factory-pattern-from-head-first-design-patterns.html?m=1
https://springframework.guru/gang-of-four-design-patterns/factory-method-design-pattern/
7. Java: Factory Design-Method Pattern | Object Oriented Design | Design Patterns
https://crunchify.com/java-factory-design-method-pattern-object-oriented-design-design-patterns/