设计模式_抽象工厂模式(4)

来源:互联网 发布:阿里云yum安装mysql 编辑:程序博客网 时间:2024/05/21 21:36
  • 什么是抽象工厂模式
  • 抽象工厂模式的例子
  • 抽象工厂模式的优缺点
  • 适用场景
  • 总结

什么是抽象工厂模式

   继续上节的情景,那个商铺的老板工作越做越好.他把那个商铺经营了一段时间,有了一定的资本.于是自己决定开个果业相关的工厂,想去获得更高的利润.很明显,他现在需要招聘更多的人来帮他管理工厂.于是乎他招聘了销售经理,生产经理,人事经理等一堆的人员,而他们又为自己招聘对应的员工.帮助他共同对这个工厂进行管理.现在对这个场景进行分析:

   抽象工厂(各种项目经理):这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在 java 中它由抽象类或者接口来实现。

   具体工厂(各个经理下面的员工):它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。在 java 中它由具体的类来实现。

   抽象商品(各个经理需要完成的任务):它是具体产品继承的父类或者是实现的接口。在 java 中一般有抽象类或者接口来实现。

   具体商品(各个员工测产出):具体工厂角色所创建的对象就是此角色的实例。在 java 中由具体的类来实现。

 

  UML图

  


抽象工厂模式的例子(此处使用以前写的一个抽象工厂模式的例子)

抽象工厂 

package com.designPattern.abstractFactory;//衣服的抽象工厂public abstract class ClothesFactory {public abstract UpperClothes createUpperClothes(int chestSize, int height);public abstract Trousers createTrousers(int waistSize, int height);} 
抽象产品1

package com.designPattern.abstractFactory;//裤子产品的接口public abstract class Trousers {public abstract int getWaistSize();public abstract int getHeight();public abstract String getName();} 
抽象产品2

package com.designPattern.abstractFactory;//抽象上衣的产品public abstract class UpperClothes {public abstract int getChestSize();public abstract int getHeight();public abstract String getName();} 

具体的产品1

package com.designPattern.abstractFactory;//西方风格的裤子public class WesternTrousers extends Trousers {private int waistSize;private int height;private String name;WesternTrousers(String name, int waistSize, int height) {this.name = name;this.waistSize = waistSize;this.height = height;}public int getHeight() {return height;}public String getName() {return name;}public int getWaistSize() {return waistSize;}}
具体工厂1

package com.designPattern.abstractFactory;//北京的衣服工厂public class BeijingClothesFactory extends ClothesFactory {public Trousers createTrousers(int waistSize, int height) {return new WesternTrousers("北京牌裤子", waistSize, height);}public UpperClothes createUpperClothes(int chestSize, int height) {return new WesternUpperClothes("北京牌上衣", chestSize, height);}}
具体的产品2

package com.designPattern.abstractFactory;//牛仔裤public class CowboyTrousers extends Trousers {private int waistSize;private int height;private String name;CowboyTrousers(String name, int waistSize, int height) {this.name = name;this.waistSize = waistSize;this.height = height;}public int getHeight() {return height;}public String getName() {return name;}public int getWaistSize() {return waistSize;}} 
具体的产品3

package com.designPattern.abstractFactory;//牛仔上衣public class CowboyUpperClothes extends UpperClothes {private int chestSize;private int height;private String name;CowboyUpperClothes(String name, int chestSize, int height) {this.name = name;this.chestSize = chestSize;this.height = height;}public int getChestSize() {return chestSize;}public int getHeight() {return height;}public String getName() {return name;}}
具体产品4
package com.designPattern.abstractFactory;//西方的上衣public class WesternUpperClothes extends UpperClothes {private int chestSize;private int height;private String name;WesternUpperClothes(String name, int chestSize, int height) {this.name = name;this.chestSize = chestSize;this.height = height;}public int getChestSize() {return chestSize;}public int getHeight() {return height;}public String getName() {return name;}} 
  

抽象工厂模式的优缺点

  优点:抽象工厂模式除了具有工厂方法模式的优点外,最主要的优点就是可以在类的内部对产品族进行约束。所谓的产品族,一般或多或少的都存在一定的关联,抽象工厂模式就可以在类内部对产品族的关联关系进行定义和描述,而不必专门引入一个新的类来进行管理。

   缺点:产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。


适用场景

  当需要创建的对象是一系列相互关联或相互依赖的产品族时,便可以使用抽象工厂模式。说的更明白一点,就是一个继承体系中,如果存在着多个等级结构(即存在着多个抽象类),并且分属各个等级结构中的实现类之间存在着一定的关联或者约束,就可以使用抽象工厂模式。假如各个等级结构中的实现类之间不存在关联或约束,则使用多个独立的工厂来对产品进行创建,则更合适一点。


总结

   无论是简单工厂模式,工厂方法模式,还是抽象工厂模式,他们都属于工厂模式,在形式和特点上也是极为相似的,他们的最终目的都是为了解耦。在使用时,我们不必去在意这个模式到底工厂方法模式还是抽象工厂模式,因为他们之间的演变常常是令人琢磨不透的。经常你会发现,明明使用的工厂方法模式,当新需求来临,稍加修改,加入了一个新方法后,由于类中的产品构成了不同等级结构中的产品族,它就变成抽象工厂模式了;而对于抽象工厂模式,当减少一个方法使的提供的产品不再构成产品族之后,它就演变成了工厂方法模式。

  所以,在使用工厂模式时,只需要关心降低耦合度的目的是否达到了。

   ps:工厂模式就看起来如此多的样式,在我们日常的开发中,只要根据我们的项目规模,风格选择合适的就是最好的.