博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
工厂模式
阅读量:4029 次
发布时间:2019-05-24

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

一.工厂模式简介

工厂模式属于23种设计模式之一,属于创建性设计模式。在许多优秀的框架的源码中都有过应用。

工厂模式可以分为三大类:简单工厂模式、工厂方法模式、抽象工厂模式

二.简单工厂模式

定义:指由一个工厂对象决定创建出哪一种产品类的实例

简单工厂适用于工厂类负责创建的对象较少的场景,且客户端只需要传入工厂类的参数,对于如何创建对象的逻辑不需要关心。

我们举一个例子:华为公司是一家大型企业,既生产手机又生产电脑等科技产品,我们可以定义一个华为产品的接口 IHWProduct

public interface IHWProduct {
void produce();}

创建一个华为手机类,实现 **IHWProduct **接口:

public class HWMobile implements IHWProduct {
public void produce() {
System.out.println("生产华为手机"); }}

创建一个华为电脑类,实现 **IHWProduct **接口:

public class HWComputer implements IHWProduct {
public void produce() {
System.out.println("生产华为电脑"); }}

此时我们创建一个华为工厂类,并提供一个生产产品的方法:

public class HWFactory {
public static IHWProduct create(@NotNull String name) {
IHWProduct huawei = null; if ("computer".equals(name)) {
huawei = new HWComputer(); } else if ("mobile".equals(name)) {
huawei = new HWMobile(); } return huawei; }}

当我们在使用客户端想要售卖华为手机的时候,代码如下:

public class Test {
public static void main(String[] args) {
IHWProduct mobile = HWFactory.create("mobile"); mobile.produce(); }}

从上面的代码中,我们不难看出,如果我们的业务继续扩展,那么工厂中的create()方法也要根据产品的增加而修改代码逻辑,这样显然是不符合开闭原则的。

我们可以使用反射技术来优化上面的工厂方法:

public class HWFactory {
public static IHWProduct create(Class
clazz) {
IHWProduct huaWei = null; try {
huaWei = (IHWProduct) clazz.newInstance(); } catch (Exception e) {
e.printStackTrace(); } return huaWei; }}

修改客户端使用的代码:

public static void main(String[] args) {
IHWProduct ihwProduct = HWFactory.create(HWComputer.class); ihwProduct.produce(); }

代码优化过后,就算产品继续增加,也不需要修改工厂的create()方法了。

优点:只需要传入一个正确的参数,就可以获取你所需要的对象,并不需要知道其创建的细节

缺点:工厂类的职责过重,不易于扩展过于复杂的产品结构

三.工厂方法模式

定义:定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类,工厂方法让类的实例化推迟到子类进行。在工厂方法 模式中用户只需要关心所需产品对应的工厂,无须关心创建细节,而且加入新的产品符合开闭原则

工厂方法模式主要解决产品扩展的问题,在简单工厂中,随着产品链的丰富,如果每个产品的创建逻辑有区别的话,工厂的职责会变得越来越多,有点像万能工厂,并不便于维护。根据单一职责原则我们将职能继续拆分,专人干专事。华为手机由 华为手机工厂创建, 华为电脑由华为电脑 工厂创建,对工厂本身也做一个抽象。来看代码,先创建 IHWFactory 接口:

public interface IHWFactory {
IHWProduct create();}

再分别创建华为手机工厂类和华为电脑工厂类,分别实现 IHWFactory 接口:

public class HWComputerFactory implements IHWFactory {
public IHWProduct create() {
return new HWComputer(); }}public class HWMobileFactory implements IHWFactory {
public IHWProduct create() {
return new HWMobile(); }}

看测试方法:

public class Test {
public static void main(String[] args) {
IHWFactory ihwComFactory = new HWComputerFactory(); IHWProduct hwComputer = ihwComFactory.create(); hwComputer.produce(); IHWFactory ihwMobFactory = new HWMobileFactory(); IHWProduct hwMobile = ihwMobFactory.create(); hwMobile.produce(); }}

工厂方法适用于以下场景:

  1. 创建对象需要大量重复的代码。
  2. 客户端(应用层)不依赖于产品类实例如何被创建、实现等细节。
  3. 一个类通过其子类来指定创建哪个对象。

工厂方法也有缺点:

  1. 类的个数容易过多,增加复杂度。
  2. 增加了系统的抽象性和理解难度

四.抽象工厂模式

定义:提供一个创建一系列相关或者相互依赖的接口的对象,无须指定她们具体的类

什么意思呢?我们举个栗子:像类似华为公司这样既生产手机又生产电脑的公司就有很多,比如说苹果和小米。

这三个公司的性质相同或者相似,都能生产相同的产品,只不过产品的品牌不一样。所以我们定义一个抽象工厂类 CompanyFactory以及所能生产的产品接口IComputerIMobile

public interface CompanyFactory {
IComputer createComputer(); IMobile createMobile();}
public interface IComputer {
void produce();}public interface IMobile {
void produce();}

接着我们创建华为生产手机和电脑以及苹果生产手机和电脑的具体实现类:

public class HWComputer implements IComputer {
public void produce() {
System.out.println("生产华为电脑"); }}public class HWMobile implements IMobile {
public void produce() {
System.out.println("生产华为手机"); }}public class Appleomputer implements IComputer {
public void produce() {
System.out.println("生产苹果电脑"); }}public class AppleMobile implements IMobile {
public void produce() {
System.out.println("生产苹果手机"); }}

那么每个公司生产产品的方法都有了,就可以建立工厂开始生成了:

public class HWFactory implements CompanyFactory {
public IComputer createComputer() {
return new HWComputer(); } public IMobile createMobile() {
return new HWMobile(); }}public class AppleFactory implements CompanyFactory {
public IComputer createComputer() {
return new Appleomputer(); } public IMobile createMobile() {
return new AppleMobile(); } }

现在我们要生成苹果的手机和电脑,来看下客户端的调用:

public class Test {
public static void main(String[] args) {
CompanyFactory appleFactory = new AppleFactory(); IComputer mbp = appleFactory.createComputer(); IMobile iphone = appleFactory.createMobile(); mbp.produce(); iphone.produce(); }}

抽象工厂的缺点:

  1. 规定了所有可能被创建的产品集合,产品族中扩展新的产品困难,需要修改抽象工厂的接口。
  2. 增加了系统的抽象性和理解难度

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

你可能感兴趣的文章
关于广告、推荐中的CTR的学于思
查看>>
文本分类
查看>>
自然语言处理中的Attention机制
查看>>
pbc 的使用
查看>>
luasocket 安装遇到的问题
查看>>
lua读写redis的环境部署
查看>>
Using MIT-Scheme in MacOS X on the Command Line
查看>>
php redis 接口说明
查看>>
cocos2dx使用lua和protobuf
查看>>
lua5.2 可能会遇到的一些错误
查看>>
C语言编译过程
查看>>
stirling formula prove
查看>>
关于数字类型转化为整型的方法
查看>>
PHP 常用正则表达式整理
查看>>
自然计算
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
系统菜单
查看>>
路漫漫其修远兮,吾将上下而求索(2)
查看>>
versions mac yosemite 下崩溃的修复
查看>>