您现在的位置是:首页 > 文章详情

设计模式: Java中的工厂设计模式

日期:2018-08-21点击:516

原文链接

https://github.com/shellhub/blog/issues/22

前言

工厂设计模式(Factory Design Pattern)属于创建模式之一,工厂设计模式在JDK,Spring,Stuts被广泛使用


img_729c154388bbe30f80d0c8528bbe6878.jpe
factory-design-pattern

当一个类或者接口有多个子类,并且基于输入返回特定的子类,此时会使用工厂设计模式。这种模式负责从客户端到工厂类的实例化。

让我们首先学习如何在java中实现工厂设计模式,然后我们将研究工厂模式的优势,我们将在JDK中看到一些工厂设计模式的使用。请注意,此模式也称为工厂方法设计模式。

工厂设计模式: 超类

工厂设计模式中的超类可以是接口,抽象类或普通的java类。对于我们的工厂设计模式示例,我们使用带有重写的toString()方法的抽象超类进行测试。

package com.github.shellhub.model; 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(); } } 

工厂设计模式: 子类

假设我们有两个子类PCServer,具有以下实现。

PC.java

package com.github.shellhub.model; 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; } } 

Server.java

package com.github.shellhub.model; 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; } } 

工厂类

既然现在我们准备好了超类和子类,我们可以编写工厂类。以下是基本的实现。

package com.github.shellhub.factory; import com.github.shellhub.model.Computer; import com.github.shellhub.model.PC; import com.github.shellhub.model.Server; public class ComputerFactory { public static Computer getComputer(String type, String ram, String hdd, String cpu) { if ("PC".equalsIgnoreCase(type)) { return new PC(ram, hdd, cpu); } else if ("Server".equalsIgnoreCase(type)) { return new Server(ram, hdd, cpu); } return null; } } 

使用

这是一个简单的测试客户端程序,它使用上面的工厂设计模式实现。

package com.github.shellhub; import com.github.shellhub.factory.ComputerFactory; import com.github.shellhub.model.Computer; public class TestFactory { public static void main(String[] args) { Computer pc = ComputerFactory.getComputer("pc", "2 GB", "500 GB", "2.4 GHz"); Computer server = ComputerFactory.getComputer("server", "16 GB", "1 TB", "2.9 GHz"); System.out.println("Factory PC Config::" + pc); System.out.println("Factory Server Config::" + server); } } 

Output:

Factory PC Config::RAM= 2 GB, HDD=500 GB, CPU=2.4 GHz Factory Server Config::RAM= 16 GB, HDD=1 TB, CPU=2.9 GHz 

工厂设计模式的优点

  • 工厂设计模式提供了接口而不是实现的代码方法。
  • 工厂模式从客户端代码中删除实际实现类的实例化。工厂模式使我们的代码更健壮,耦合更少,易于扩展。例如,我们可以轻松更改PC类实现,因为客户端程序不知道这一点。
  • 工厂模式通过继承提供实现和客户端类之间的抽象。

工厂设计模式在JDK中的应用

  1. java.util.CalendarResourceBundleNumberFormat getInstanc()方法使用Factory模式。
  2. 包装类中的valueOf()方法,如BooleanInteger等。
原文链接:https://yq.aliyun.com/articles/638415
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章