设计模式学习笔记三——Abstract Factory模式

动机:实现一系列相互依赖对象的创建,通过封装系列对象创建来隔离对象创建和客户程序,从而能够灵活提供更多系列对象创建和系列对象间的切换,而不需要改变客户程序。

应用:多种风格软件界面控件的创建、对应多种数据库的多系列数据访问层对象的创建(比如Petshop中的数据访问层对象)。

场景:此处以ThinkPad系列产品的组装为例子。组装电脑程序(即客户)需要根据需要拿出不同系列ThinkPad(此处以T43和T60为例)部件(以CPU和主板为例)组装成ThinkPad。因为T43和T60的CPU系列接口不同,所以同一系列中的CPU和主板相互依赖,每次只能拿同一系列的CPU和主板进行组装。

Factory Method模式:Factory Method模式是Abstract Factory模式的特殊形式,它实现一个对象的创建,而Abstract Factory模式实现系列对象创建。如这里只涉及到多种CPU的创建,则变为了Factory Method模式。


结构

AbstractFactory模式结构图


代码实现

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif* 产品CPU
ExpandedBlockEnd.gif
*/

None.gif
namespace  DesignPattern.AbstractFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class CPU
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T43CPU : CPU
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T60CPU : CPU
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif* 产品主板
ExpandedBlockEnd.gif
*/

None.gif
namespace  DesignPattern.AbstractFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class Mainboard
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T43Mainboard : Mainboard
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T60Mainboard : Mainboard
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * 不同ThinkPad系列相互依赖部件组成不同型号ThinkPad,此处忽略各种不同型号。
ExpandedBlockEnd.gif
*/

None.gif
namespace  DesignPattern.AbstractFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ThinkPad
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void Play()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * ThinkPad工厂,生产各型号ThinkPad部件
ExpandedBlockEnd.gif
*/

None.gif
namespace  DesignPattern.AbstractFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class ThinkPadFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public abstract CPU CreateCPU();
InBlock.gif        
public abstract Mainboard CreateMainboard();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T43Factory : ThinkPadFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public override CPU CreateCPU()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new T43CPU();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override Mainboard CreateMainboard()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new T43Mainboard();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class T60Factory : ThinkPadFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public override CPU CreateCPU()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new T60CPU();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override Mainboard CreateMainboard()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new T60Mainboard();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif* 客户程序,从ThinkPad工厂中拿出所需系列部件进行组装。
ExpandedBlockEnd.gif
*/

None.gif
namespace  DesignPattern.AbstractFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ThinkPadAssembly
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static readonly ThinkPadAssembly Instanse = new ThinkPadAssembly();
InBlock.gif
InBlock.gif        
public ThinkPad CreateThinkPad(string strThinkPadName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ThinkPadFactory thinkPadFactory 
= (ThinkPadFactory)Assembly.Load("DesignPattern.AbstractFactory").CreateInstance("DesignPattern.AbstractFactory" + "." + strThinkPadName + "Factory");
InBlock.gif            CPU cpu 
= thinkPadFactory.CreateCPU();
InBlock.gif            Mainboard mainboard 
= thinkPadFactory.CreateMainboard();
InBlock.gif
InBlock.gif            
return Assemble(cpu, mainboard);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private ThinkPad Assemble(CPU cpu, Mainboard mainboard)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 组装并返回ThinkPad,此处简化各种型号的ThinkPad
InBlock.gif
            return new ThinkPad();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Charly/archive/2007/05/29/763697.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值