Simple Factory設計模式

來源:互聯網
上載者:User

 

原廠模式專門負責將大量有共同介面的類執行個體化。原廠模式可以動態決定將哪一個類執行個體化,不必事Crowdsourced Security Testing道每次要執行個體化哪一個類。原廠模式有以下幾種形態:

簡單工廠(Simple Factory)模式

Factory 方法(Factory Method)模式

抽象工廠(Abstract Factory)模式

一、 簡單工廠(Simple Factory)模式

Simple Factory模式根據提供給它的資料,返回幾個可能類中的一個類的執行個體。通常它返回的類都有一個公用的父類和公用的方法。

Simple Factory模式實際上不是GoF 23個設計模式中的一員。

二、 Simple Factory模式角色與結構:

工廠類角色Creator (LightSimpleFactory):工廠類在用戶端的直接控制下(Create方法)建立產品對象。

抽象產品角色Product (Light):定義簡單工廠建立的對象的父類或它們共同擁有的介面。可以是一個類、抽象類別或介面。

具體產品角色ConcreteProduct (BulbLight, TubeLight):定義工廠具體加工出的對象。

using System;  

public abstract class Light  
{  
   public abstract void TurnOn();  
   public abstract void TurnOff();  
}  

public class BulbLight : Light  
{  
   public override void TurnOn()  
   {  
      Console.WriteLine("Bulb Light is Turned on");  
   }  

   public override void TurnOff()  
   {  
      Console.WriteLine("Bulb Light is Turned off");  
   }  
}  

public class TubeLight : Light  
{  
   public override void TurnOn()  
   {  
      Console.WriteLine("Tube Light is Turned on");  
   }  

   public override void TurnOff()  
   {  
      Console.WriteLine("Tube Light is Turned off");  
   }  
}  

public class LightSimpleFactory  
{  
   public Light Create(string LightType)  
   {  
      if(LightType == "Bulb")  
         return new BulbLight();  
      else if(LightType == "Tube")  
         return new TubeLight();  
      else
         return null;  
   }  
}  

public class Client  
{  
   public static void Main()  
   {  
      LightSimpleFactory lsf = new LightSimpleFactory();  

      Light l = lsf.Create("Bulb");  
      l.TurnOn();  
      l.TurnOff();  

      Console.WriteLine("-----------------");  

      l = lsf.Create("Tube");  
      l.TurnOn();  
      l.TurnOff();  
   }  
}

 

四、 Simple Factory模式演化

Simple Factory模式演化(一)

除了上面的用法外,在有些情況下Simple Factory可以由抽象產品角色扮演,一個抽象產品類同時是子類的工廠。

using System;  

public class Light  
{  
   public virtual void TurnOn()  
   {  
   }  

   public virtual void TurnOff()  
   {  
   }  

   public static Light Create(string LightType)  
   {  
      if(LightType == "Bulb")  
         return new BulbLight();  
      else if(LightType == "Tube")  
         return new TubeLight();  
      else
         return null;  
   }  
}  

public class BulbLight : Light  
{  
   public override void TurnOn()  
   {  
      Console.WriteLine("Bulb Light is Turned on");  
   }  

   public override void TurnOff()  
   {  
      Console.WriteLine("Bulb Light is Turned off");  
   }  
}  

public class TubeLight : Light  
{  
   public override void TurnOn()  
   {  
      Console.WriteLine("Tube Light is Turned on");  
   }  

   public override void TurnOff()  
   {  
      Console.WriteLine("Tube Light is Turned off");  
   }  
}  

public class Client  
{  
   public static void Main()  
   {  
      Light l = Light.Create("Bulb");  
      l.TurnOn();  
      l.TurnOff();  

      Console.WriteLine("-----------------");  

      l = Light.Create("Tube");  
      l.TurnOn();  
      l.TurnOff();  
   }  
}

Simple Factory模式演化(二)

三個角色全部合并:

與單件模式(Singleton)相近,但是有區別。

五、 優點與缺點:
優點:
工廠類含有必要的判斷邏輯,可以決定在什麼時候建立哪一個產品類的執行個體,用戶端可以免除直接建立產品對象的責任,而僅僅"消費"產品。簡單原廠模式通過這種做法實現了對責任的分割。

缺點:
當產品有複雜的多層等級結構時,工廠類只有自己,以不變應萬變,就是模式的缺點。因為工廠類集中了所有產品建立邏輯,一旦不能正常工作,整個系統都要受到影響。

同時,系統擴充困難,一旦添加新產品就不得不修改工廠邏輯,有可能造成工廠邏輯過於複雜。

另外,簡單原廠模式通常使用靜態Factory 方法,這使得無法由子類繼承,造成工廠角色無法形成基於繼承的等級結構。

 

轉自:http://hi.baidu.com/allyang007/blog/item/d7d572258a523d3a8744f990.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.