Abstract Factory 抽象工厂模式(创建型模式)

  说到对象的创建,我们第一想到的就是New。但New带来的问题是实现依赖,不能应对“具体实例类型”的变化。创建型模式其实就是解决New的问题。
哪里变化,封装哪里。我们认为对象的创建存在变化,因为我们封装对象创建。
例1:对象创建

None.gif class  Road
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif   Road road 
= new Road();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

若路是经常变化的,则路的创建是变化的,封装变化点。new是变化点。
例2:封装对象创建

None.gif class  Road
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  RoadFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
public static Road CreateRoad()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif      
return new Road();
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
None.gif
class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      Road road 
= RoadFactory.CreateRoad();
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

RoadFactory是变化点。
例2:创建一系列相互依赖的对象

None.gif class  Wall
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  Window
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  Floor
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
class  House
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   Wall w;
InBlock.gif   Window wi;
InBlock.gif   Floor f;
InBlock.gif   
public House(Wall w,Window wi,Floor f)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     
this.w=w;this.wi=wi;this.f=f;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
class  HouseFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
public static Wall CreateWall()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Wall();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public static Window CreateWindow()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Window();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public static FloorCreateFloor()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Floor();
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
// 创建房子,由墙,窗户,地板组成
None.gif
class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     Wall w 
= HouseFactory.CreateWall();
InBlock.gif     Window wi 
= HouseFactory.CreateWindow();
InBlock.gif     Floor f 
= HouseFactory.CreateFloor();
InBlock.gif     House h 
= new House(w,wi,f);
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

若需要创建不同风格的House,如古典房子:砖墙、木窗、砖地板。现代房子:水泥墙、铝窗、水泥
地板。这就是简单(静态)工厂模式的问题所在,不能应对“不同系列对象”的变化。HouseFactory是变化点,这是抽象工厂模式需要解决的问题。
例3:抽象工厂

None.gif public   abstract   class  Wall
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif
Wall #region Wall
InBlock.gif
public class Wallx:Wall
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
public class Wally:Wall
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion

None.gif
public   abstract   class  Window
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif
Window #region Window
InBlock.gif
public class Windowx:Window
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
public class Windowy:Window
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion

None.gif
public   abstract   class  Floor
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif
Floor #region Floor
InBlock.gif
public class Floorx:Floor
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
public class Floory:Floor
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

ContractedSubBlock.gifExpandedSubBlockStart.gif
dot.gif#region
InBlock.gif
public class House
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif   Wall w;
InBlock.gif   Window wi;
InBlock.gif   Floor f;
InBlock.gif   
public House(Wall w,Window wi,Floor f)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     
this.w=w;this.wi=wi;this.f=f;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif}

InBlock.gif
//抽象工厂
InBlock.gif
public abstract class HouseFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
public abstract Wall CreateWall();
InBlock.gif  
public abstract Window CreateWindow();
InBlock.gif  
public abstract Floor CreateFloor();
ExpandedSubBlockEnd.gif}

ContractedSubBlock.gifExpandedSubBlockStart.gif
HouseFactory#region HouseFactory
InBlock.gif
class HouseFactoryx:HouseFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
public override Wall CreateWall()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Wallx();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public override Window CreateWindow()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Windowx();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public override Floor CreateFloor()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Floorx();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.gif
class HouseFactoryy:HouseFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
public override Wall CreateWall()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Wally();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public override Window CreateWindow()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Windowy();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public override Floor CreateFloor()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     
return new Floory();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion

InBlock.gif
//创建房子,由墙x,窗户x,地板x或墙y,窗户y,地板y组成。代有2个系列。
InBlock.gif
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif     HouseFactory houseFactory 
= new HouseFactoryx();
InBlock.gif     
//HouseFactory houseFactory = new HouseFactoryy();
InBlock.gif
     Wall w = houseFactory.CreateWall();
InBlock.gif     Window wi 
= houseFactory.CreateWindow();
InBlock.gif     Floor f 
= houseFactory.CreateFloor();
InBlock.gif     House h 
= new House(w,wi,f);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

 
  生成x系列的House还是y系列的房子,客户程序仅需要修改一行代码。其实把这种改变写到配置文件中将不需要重新编译代码就能得到不同系列的房子。利用反射技术创建HouseFactory的对象。

一、动机(Motivation)
   在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化往往存在“更多系列对象”的创建工作。
 如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种多系列具体对象创建工作的紧密耦合?
二、意图(Intent)
  提供一个接口,让该接口负责创建一系列相关或者相互依赖的对象,无需指定它们具体的类。
三、结构(Structure)
PIC034.jpg
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


PS:
如果没有应对“多系列对象构建”的需求变化,则没有必要使用abstract factory模式,这时使用简单的静态工厂完全可以。
“系列对象”指的是这些对象之间有相互依赖,或作用的关系。
abstract factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动。

转载于:https://www.cnblogs.com/hotsoho.net/articles/328893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值