博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式: Java中的工厂设计模式
阅读量:7031 次
发布时间:2019-06-28

本文共 2888 字,大约阅读时间需要 9 分钟。

原文链接

前言

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

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

让我们首先学习如何在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 GHzFactory Server Config::RAM= 16 GB, HDD=1 TB, CPU=2.9 GHz复制代码

工厂设计模式的优点

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

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

  1. java.util.CalendarResourceBundleNumberFormat getInstanc()方法使用Factory模式。
  2. 包装类中的valueOf()方法,如BooleanInteger等。

转载地址:http://ajwal.baihongyu.com/

你可能感兴趣的文章
【100题】第五十九题 用C++编写不能被继承的类
查看>>
轻描淡写
查看>>
mysql基本操作
查看>>
39.CSS3弹性伸缩布局【下】
查看>>
[javascript]图解+注释版 Ext.extend()
查看>>
我的前端工具集(七)div背景网格
查看>>
linux 下mongo 基础配置
查看>>
【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)...
查看>>
JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结
查看>>
java之jvm学习笔记六(实践写自己的安全管理器)
查看>>
Docker容器查看ip地址
查看>>
在PC端或移动端应用中接入商业QQ
查看>>
将python3.6软件的py文件打包成exe程序
查看>>
DataTable 排序
查看>>
大白话5分钟带你走进人工智能-第二十节逻辑回归和Softmax多分类问题(5)
查看>>
嵌入式系统在工业控制中的应用
查看>>
使用httpclient异步调用WebAPI接口
查看>>
c++ 类的对象与指针
查看>>
SSTI(模板注入)
查看>>
rbac models
查看>>