软件工程 - 设计模式学习之工厂方法模式Factory Method

在现实生活中我们可以看见,乐事在卖薯片,可比克也在卖.
我敢肯定的说它们各自都有自己的工厂来生产薯片,而且生产出来的薯片味道还是不一样的.
这就是我们这里所谓的工厂方法模式. 我们来看看这个模式的UML图:

 

这个模式中我们可以看到:
产品和工厂2个基类,然后它们下面又有些子孙类.
--------------------------------------------------------------------------------
现在进入我们的代码环节了,首先上场的是PHP. 

ExpandedBlockStart.gif 代码
 1  // 抽象产品   
 2  abstract   class  Product{   
 3       abstract   function  Test();   
 4  }   
 5    
 6  class  leshi  extends  Product {   
 7       public   function  Test() {   
 8           echo ( " Leshi!\n " );   
 9      }   
10  }   
11  class  kebike  extends  Product {   
12       public   function  Test() {   
13           echo ( " Kebike!\n " );   
14      }   
15  }   
16  // 抽象工厂   
17  abstract   class  Factory{   
18       abstract   function  GetProduct();   
19  }   
20    
21  class  leshiFactory  extends  Factory{     
22       public   function  GetProduct() {   
23           return   new  leshi();   
24      }   
25  }   
26  class  kebikeFactory  extends   Factory{   
27       public   function  GetProduct() {   
28           return   new  kebike();   
29      }   
30  }   
31  // 首先我们先建立两个公司的工厂.   
32  $leshiF   =   new  leshiFactory();   
33  $kebikeF   =   new  kebikeFactory();   
34  // 每个工厂给我出10包薯片   
35  for ( $i = 0 ; $i < 10 ; $i ++ ){   
36       $leshiF -> GetProduct() -> Test();   
37       $kebikeF -> GetProduct() -> Test();   
38  }  
39 
40  // 抽象产品
41  abstract   class  Product{
42       abstract   function  Test();
43  }
44 
45  class  leshi  extends  Product {
46       public   function  Test() {
47           echo ( " Leshi!\n " );
48      }
49  }
50  class  kebike  extends  Product {
51       public   function  Test() {
52           echo ( " Kebike!\n " );
53      }
54  }
55  // 抽象工厂
56  abstract   class  Factory{
57       abstract   function  GetProduct();
58  }
59 
60  class  leshiFactory  extends  Factory{  
61       public   function  GetProduct() {
62           return   new  leshi();
63      }
64  }
65  class  kebikeFactory  extends   Factory{
66       public   function  GetProduct() {
67           return   new  kebike();
68      }
69  }
70  // 首先我们先建立两个公司的工厂.
71  $leshiF   =   new  leshiFactory();
72  $kebikeF   =   new  kebikeFactory();
73  // 每个工厂给我出10包薯片
74  for ( $i = 0 ; $i < 10 ; $i ++ ){
75       $leshiF -> GetProduct() -> Test();
76       $kebikeF -> GetProduct() -> Test();
77  }
78 
79 

 

和简单工厂模式有点儿像,但是那个只有一个工厂实例,
而我们这里是有多少种产品就有多少个工厂实例
--------------------------------------------------------------------------------
 C#粉墨登场:

ExpandedBlockStart.gif 代码
namespace  FactoryMethod_DesignPattern   
{   
    
using  System;   
  
    
//  These two classes could be part of a framework,   
    
//  which we will call DP   
    
//  ===============================================   
       
    
class  DPDocument    
    {   
       
  
    }   
  
    
abstract   class  DPApplication    
    {   
        
protected  DPDocument doc;   
           
        
abstract   public   void  CreateDocument();   
  
        
public   void  ConstructObjects()   
        {   
               
            
//  Create objects as needed   
            
//  . . .   
  
            
//  including document   
            CreateDocument();   
        }          
        
abstract   public   void  Dump();   
    }   
  
    
//  These two classes could be part of an application    
    
//  =================================================   
  
    
class  MyApplication : DPApplication    
    {   
        
override   public   void  CreateDocument()   
        {   
            doc 
=   new  MyDocument();            
        }              
  
        
override   public   void  Dump()   
        {   
            Console.WriteLine(
" MyApplication exists " );   
        }   
    }      
  
    
class  MyDocument : DPDocument    
    {   
  
    }   
  
    
///   <SUMMARY></SUMMARY>    
    
///     Summary description for Client.   
    
///     
     public   class  Client   
    {   
        
public   static   int  Main( string [] args)   
        {   
            MyApplication myApplication 
=   new  MyApplication();   
  
            myApplication.ConstructObjects();   
  
            myApplication.Dump();   
               
            
return   0 ;   
        }   
    }   
}  

namespace  FactoryMethod_DesignPattern
{
    
using  System;

 
//  These two classes could be part of a framework,
 
//  which we will call DP
 
//  ===============================================
 
 
class  DPDocument 
 {
 

 }

 
abstract   class  DPApplication 
 {
  
protected  DPDocument doc;
  
  
abstract   public   void  CreateDocument();

  
public   void  ConstructObjects()
  {
   
   
//  Create objects as needed
   
//  . . .

   
//  including document
   CreateDocument();
  }  
  
abstract   public   void  Dump();
 }

 
//  These two classes could be part of an application 
 
//  =================================================

 
class  MyApplication : DPApplication 
 {
  
override   public   void  CreateDocument()
  {
   doc 
=   new  MyDocument();   
  }   

  
override   public   void  Dump()
  {
   Console.WriteLine(
" MyApplication exists " );
  }
 } 

 
class  MyDocument : DPDocument 
 {

 }

    
///  
    
///     Summary description for Client.
    
///  
     public   class  Client
    {
        
public   static   int  Main( string [] args)
        {
            MyApplication myApplication 
=   new  MyApplication();

   myApplication.ConstructObjects();

   myApplication.Dump();
   
            
return   0 ;
        }
    }
}

 

--------------------------------------------------------------------------------
Delphi王者驾临!

 

ExpandedBlockStart.gif Delphi
IProduct  =   interface  
    
procedure  Test; 
  
end

  IFactory 
=   interface  
    
function  GetProduct:IProduct; 
  
end

  TLeShi 
=   class (TInterfacedObject,IProduct) 
  
public  
    
procedure  Test; 
  
end

  TKeBiKe 
=   class (TInterfacedObject,IProduct) 
  
public  
    
procedure  Test; 
  
end

  TLeShiFactory 
=   class (TInterfacedObject,IFactory) 
  
public  
    
function  GetProduct:IProduct; 
  
end

  TKeBiKeFactory 
=   class (TInterfacedObject,IFactory) 
  
public  
    
function  GetProduct:IProduct; 
  
end

var  
  Form1: TForm1; 

implementation  

{ $R *.dfm }  

{  TKeBiKeFactory  }  

function  TKeBiKeFactory.GetProduct: IProduct; 
begin  
  Result:
= TKeBiKe.Create; 
end

{  TLeShi  }  

procedure  TLeShi.Test; 
begin  
  ShowMessage(
' LeShi! ' ); 
end

{  TKeBiKe  }  

procedure  TKeBiKe.Test; 
begin  
  ShowMessage(
' KeBiKe ' ); 
end

{  TLeShiFactory  }  

function  TLeShiFactory.GetProduct: IProduct; 
begin  
  Result:
= TLeShi.Create; 
end

 

--------------------------------------------------------------------------------
给乐事和可比克打了广告不知道能不能得到点儿广告费哟..哈哈.
我们最后看看这个模式的适用性:

当一个类不知道它所必须创建的对象的类的时候。
当一个类希望由它的子类来指定它所创建的对象的时候。
当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值