設計模式C#描述——簡單原廠模式

來源:互聯網
上載者:User
      前 言:設計模式是軟體開發領域的精髓之一。學好設計模式是目前每一個開發人員的必修課。目前關於設計模式的書很多,其中比較好的有GOF那本的中譯本,但並 不很適合初學者。還有一本是《JAVA與模式》,比較適合初學者使用,在此強烈推薦。但這本書的不足之處是一些地方講的過於繁瑣,很多地方只須簡單說明一 下即可,卻大費筆墨,使得書籍很厚,看起來費力。而且是用JAVA描述的,這使得一些只懂C#的人無從下手。我是一個.net的擁護者,為了看這本書我特 意看了些JAVA的書,感覺JAVA在書籍的多樣性方面比 .net好很多,而且不少書籍的品質很高。可能是現在JAVA已經比較成熟的原因吧。為了方便.net的愛好者學習設計模式,在此把我學習《JAVA與模 式》這本書的學習筆記發出來,並用C#語言重新描述,希望能對初學者有所協助。
其實設計模式也並不是什麼高深的理論,個人認為並不是象一些人所說的“沒寫過10萬代碼就不要談設計模式”,只要用心學習與實踐是完全能夠掌握的。

簡單原廠模式是類的建立模式,又叫做靜態Factory 方法模式。就是由一個工廠類根據傳入的參量決定建立出哪一種產品類的執行個體。一般涉及到三種角色(如):

工廠類:擔任這個角色的是Factory 方法模式的核心,含有與應用緊密相關的商業邏輯。工廠類在用戶端的直接調用下建立產品對象,它往往由一個具體的類實現。
抽象產品角色:擔任這個角色的類是由Factory 方法模式所建立的對象的父類,或她們共同擁有的介面。一般由介面或抽象類別實現。
具體產品角色:Factory 方法模式所建立的任何對
象都是這個角色的執行個體,由具體類實現。

簡單原廠模式優缺點:
模式的核心是工廠類,這個類負責產品的建立,而用戶端可以免去產品建立的責任,這實現了責任的分割。但由於工廠類集中了所有產品建立邏輯的,如果不能正常工作的話會對系統造成很大的影響。如果增加新產品必須修改工廠角色的源碼。

以園丁種植水果為例討論該模式的具體實現:
Fruit 水果介面,規定水果具有的一些共同特性
Apple 蘋果類 派生自Fruit介面
Strawberry 草莓類 派生自Fruit介面
FruitGardener 園丁類 負責草莓與蘋果的建立工作。
當Client要建立水果(蘋果或草莓對象)的時候調用園丁類的factory方法建立:UML圖如下:

代碼如下:
Fruit.cs
namespace Simple_Factory
{
public interface Fruit
{
//生長
void grow();
//收穫
void harvest();
//種植
void plant();
}
}
Apple.cs
namespace Simple_Factory
{
public class Apple:Fruit
{
public Apple()
{
}
#region Fruit 成員
public void grow()
{
Console.WriteLine ("Apple is growing.......");
}
public void harvest()
{
Console.WriteLine ("Apple is harvesting.......");
}
public void plant()
{
Console.WriteLine ("Apple is planting.......");
}
#endregion
}
}
Strawberry.cs
namespace Simple_Factory
{
public class Strawberry:Fruit
{
public Strawberry()
{
}
#region Fruit 成員
public void grow()
{
Console.WriteLine ("Strawberry is growing.......");
}
public void harvest()
{
Console.WriteLine ("Strawberry is harvesting.......");
}
public void plant()
{
Console.WriteLine ("Strawberry is planting.......");
}
#endregion
}
}
FruitGardener.cs
namespace Simple_Factory
{
public class FruitGardener
{
//靜態Factory 方法
public static Fruit factory(string which)
{
if(which.Equals ("Apple"))
{
return new Apple();
}
else if(which.Equals ("Strawberry"))
{
return new Strawberry ();
}
else
{
return null;
}
}
}
}
Client.cs
using System;
namespace Simple_Factory
{
class Client
{
[STAThread]
static void Main(string[] args)
{
Fruit aFruit=FruitGardener.factory ("Apple");//creat apple
aFruit.grow ();
aFruit.harvest ();
aFruit.plant();
aFruit=FruitGardener.factory ("Strawberry");//creat strawberry
aFruit.grow ();
aFruit.harvest ();
aFruit.plant();
}
}
}
輸出如下:
Apple is growing.......
Apple is harvesting.......
Apple is planting.......
Strawberry is growing.......
Strawberry is harvesting.......
Strawberry is planting.......

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.