Java工厂设计模式

编程

工厂模式很普遍,想必好多人一开始接触的就是这个模式了

工厂方法模式一种创建对象的模式,它被广泛应用在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();

}

}

 工厂模式子类,有四种实现:

publicclassPCextendsComputer{

private String ram;

private String hdd;

private String cpu;

publicPC(String ram, String hdd, String cpu){

this.ram=ram;

this.hdd=hdd;

this.cpu=cpu;

}

@Override

public String getRAM(){

returnthis.ram;

}

@Override

public String getHDD(){

returnthis.hdd;

}

@Override

public String getCPU(){

returnthis.cpu;

}

}

publicclassPadextendsComputer{

private String ram;

private String hdd;

private String cpu;

publicPad(String ram, String hdd, String cpu){

this.ram=ram;

this.hdd=hdd;

this.cpu=cpu;

}

@Override

public String getRAM(){

returnthis.ram;

}

@Override

public String getHDD(){

returnthis.hdd;

}

@Override

public String getCPU(){

returnthis.cpu;

}

}

publicclassMobilePhoneextendsComputer{

private String ram;

private String hdd;

private String cpu;

publicMobilePhone(String ram, String hdd, String cpu){

this.ram=ram;

this.hdd=hdd;

this.cpu=cpu;

}

@Override

public String getRAM(){

returnthis.ram;

}

@Override

public String getHDD(){

returnthis.hdd;

}

@Override

public String getCPU(){

returnthis.cpu;

}

}

publicclassServerextendsComputer{

private String ram;

private String hdd;

private String cpu;

publicServer(String ram, String hdd, String cpu){

this.ram=ram;

this.hdd=hdd;

this.cpu=cpu;

}

@Override

public String getRAM(){

returnthis.ram;

}

@Override

public String getHDD(){

returnthis.hdd;

}

@Override

public String getCPU(){

returnthis.cpu;

}

}

 定义工厂类

现在有了多个子类和超类,接下来可以创建工厂类了:

/**

*

* @author dgm

* @describe "产品类型"

* @date 2020年5月12日

*/

publicenum ComputerType {

PC, SERVER, PAD, MOBILEPHONE

}

/**

*

* @author dgm

* @describe "生产产品的工厂,类似富士康"

* @date 2020年5月12日

*/

publicclassComputerFactory{

publicstatic 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 是工厂类的方法,且基于相同的参数类型返回了不同的对象;

接下来是一个简单的测试客户端程序,它使用上面的设计模式" title="工厂设计模式">工厂设计模式实现。

 

/**

*

* @author dgm

* @describe "测试工厂生产的产品合格吗"

* @date 2020年5月12日

*/

publicclassTestFactory{

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

}

}

输出结果:

 

很好四款产品都合格,没有劣质产品

工厂设计模式的优点

  • 面向接口编程,体现了面向对象的思想;
  • 将创建对象的工作转移到了工厂类,不用自己new了,委托给工厂生产合格产品

JDK 中的工厂设计模式实例

  • java.util.Calendar, ResourceBundle and NumberFormat getInstance() 使用了工厂方法模式;
  • valueOf() 在包装类中,如Boolean, Integer 也使用了工厂方法模式;

工厂模式在框架里的体现

  • Mybatis: DataSourceFactory,SqlSessionFactory,MapperProxyFactory很重要

  • Spring: 一系类***BeanFactory

工厂模式在以前开发中的应用场景:

  • 数据库连接类型,数据库连接工厂通过参数化可返回具体的数据源

  • 存储系统开发时协议,  nfs,cifs,ftp等

  • 新疆安防,不同厂商ABCDE等的监控设备对接
  • 支付方式,客户购买服务后,支付类型的选择支付宝、银联,可以继续追加其他支付类型
  • 数据导入导出,json,txt,excel等
  • 。。。。。。工厂模式太普遍了,不一一举例了

 

小结:  工厂模式很好用,也很实用!!!师傅领进门,修行靠个人,不要老背代码(看到不少人喜欢背代码却不会变通),要想不同的模式在场景怎么使用、如何使用、使用哪种模式,不是靠死记硬背,当然了也许他们搞明白概念,加油!!!

附代码已上传 https://github.com/dongguangming/design-pattern/tree/master/src/code/factory

参考:

0. java factory design pattern https://www.w3spoint.com/java-factory-design-pattern

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

3、Factory Design Pattern in Java 

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

6. Factory Method Design Pattern 

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/

 

以上是 Java工厂设计模式 的全部内容, 来源链接: utcz.com/z/516409.html

回到顶部