C#設(shè)計模式之簡單工廠模式
設(shè)計模式分類:
- 創(chuàng)建型模式。
- 結(jié)構(gòu)型模式。
- 行為模式。
23種設(shè)計模式,如何記。面向?qū)ο蟮南到y(tǒng)中有很多對象,創(chuàng)建型模式解決的問題就是如何創(chuàng)建對象,何時創(chuàng)建對象,它努力的讓代碼不要太多的關(guān)注對象的具體類型,不用關(guān)注對象的創(chuàng)建細節(jié),而知需要了解對象的抽象類型,創(chuàng)建對象的工作由創(chuàng)建對象的工廠來實現(xiàn)。
面向?qū)ο蟮南到y(tǒng)中,對象如何組織,采用什么樣的結(jié)構(gòu)組織比較合理,這個是由結(jié)構(gòu)型模式來處理的。合理的使用結(jié)構(gòu)型模式可以使系統(tǒng)具備更好的靈活性、擴展性和維護性。
行為模式規(guī)定了各個對象間的應(yīng)該具備的職責。
嚴格來說,簡單工廠模式并不是23種標準模式之一,它是工廠家族中最簡單的模式,使用這個模式可以把對象的創(chuàng)建和對象的使用分離開,工廠只負責對象的創(chuàng)建,客戶端程序調(diào)用和使用對象,客戶端程序無需創(chuàng)建對象。這樣對象的創(chuàng)建放在一起,方便維護和擴展。現(xiàn)實中生產(chǎn)鞋子的工廠負責生產(chǎn)鞋子,我們不需要知道生產(chǎn)的鞋子的具體類型。

如圖所示:右上角是一個產(chǎn)品接口,我們可以使用接口或抽象類來定義一個產(chǎn)品對象。Animal類中有一個行為吃,Animal類派生出兩個子類:Dog、Penguin。這兩個類分別實現(xiàn)了吃的動作,這兩個動物其實是簡單工廠中具體的產(chǎn)品,通過他們實現(xiàn)抽象的產(chǎn)品;這些動物該如何去創(chuàng)建呢,我們可以用動物工廠AnimalFactory來創(chuàng)建具體的動物,AnimalFactory類中有一個GetAnimal的方法,在這個方法里我們可以根據(jù)傳進去的參數(shù)來創(chuàng)建具體的產(chǎn)品,工廠類和產(chǎn)品類是依賴的關(guān)系。
在客戶端,它關(guān)聯(lián)了抽象的產(chǎn)品類Animal和工廠類AnimalFactory,對客戶來說,他不需要了解具體的產(chǎn)品,只需要告訴工廠,需要什么具體的動物,動物工廠就會根據(jù)客戶端的要求來創(chuàng)建某個動物,通過簡單工廠模式,使客戶端程序與具體的產(chǎn)品之間減少耦合度。
示例:
抽象動物類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/*
動物抽象類
* 抽象產(chǎn)品
*/
public abstract class Animal
{
public abstract void Eat();
}
}具體動物狗類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/*
具體的產(chǎn)品類,實現(xiàn)抽象產(chǎn)品類
*/
public class Dog:Animal
{
// 實現(xiàn)抽象方法
public override void Eat()
{
Console.WriteLine("狗在吃飯!");
}
}
}具體動物企鵝類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/*
具體產(chǎn)品類,實現(xiàn)抽象產(chǎn)品類
*/
public class Penguin :Animal
{
// 實現(xiàn)抽象方法
public override void Eat()
{
Console.WriteLine("企鵝在吃飯!");
}
}
}動物工廠類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/*
動物工廠類:實現(xiàn)具體的動物
*/
public class AnimalFactory
{
/// <summary>
/// 根據(jù)客戶的選擇創(chuàng)建動物對象
/// </summary>
/// <param name="witch">動物編號</param>
/// <returns></returns>
public Animal GetAnimal(int witch)
{
Animal am = null;
switch (witch)
{
case 1:
am = new Dog();
break;
case 2:
am = new Penguin();
break;
}
return am;
}
}
}主程序調(diào)用測試
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
class Program
{
static void Main(string[] args)
{
// 得到具體的動物 (里氏替換原則)
Animal am = new AnimalFactory().GetAnimal(1);
// 調(diào)用Eat()方法
am.Eat(); // 輸出狗在吃飯
Console.ReadKey();
}
}
}測試結(jié)果:

使用接口實現(xiàn)簡單工廠模式
需求:使用面向?qū)ο蟮姆绞皆O(shè)計一個系統(tǒng),描述使用卡車從事貨運,使用公共汽車從事客運。使用工廠模式實現(xiàn)。
1、汽車接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/// <summary>
/// 汽車接口
/// </summary>
public interface ICar
{
/// <summary>
/// 描述汽車從事的活動
/// </summary>
void Work();
}
}2、分別定義卡車類(Truck)和公共汽車類(Bus)實現(xiàn)ICar接口
Truck類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/// <summary>
/// 卡車類
/// </summary>
public class Truck : ICar
{
/// <summary>
/// 卡車從事的活動
/// </summary>
public void Work()
{
Console.WriteLine("卡車從事貨運");
}
}
}Bus類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/// <summary>
/// 公共汽車類
/// </summary>
public class Bus:ICar
{
/// <summary>
/// 公共汽車從事的活動
/// </summary>
public void Work()
{
Console.WriteLine("公共汽車從事客運");
}
}
}3、定義汽車的工廠類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
/// <summary>
/// 汽車工廠類:返回具體的汽車類
/// </summary>
public class CarFactory
{
/// <summary>
/// 根據(jù)汽車編號創(chuàng)建具體的汽車對象
/// </summary>
/// <param name="witch">汽車編號</param>
/// <returns></returns>
public ICar GetCar(int witch)
{
ICar car = null;
switch (witch)
{
case 1:
car= new Truck();
break;
case 2:
car = new Bus();
break;
}
return car;
}
}
}4、主程序調(diào)用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 簡單工廠模式
{
class Program
{
static void Main(string[] args)
{
// 得到具體的汽車
ICar car = new CarFactory().GetCar(2);
// 調(diào)用Work()方法
car.Work();
Console.ReadKey();
}
}
}5、程序運行結(jié)果

簡單工廠模式的缺點:
增加具體產(chǎn)品時,需要修改工廠類里面生成具體產(chǎn)品的方法,這就違反了開閉原則。具體產(chǎn)品經(jīng)常發(fā)生變化時,不建議使用簡單工廠模式。
代碼下載地址:點擊下載
到此這篇關(guān)于C#設(shè)計模式之簡單工廠模式的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C#如何利用TcpListener和TcpClient實現(xiàn)Tcp通訊
TcpListener 和 TcpClient 是在 System.Net.Sockets.Socket 類的基礎(chǔ)上做的進一步封裝,使用 GetStream 方法返回網(wǎng)絡(luò)流,下面我們就來詳細一下如何使用TcpListener和TcpClient實現(xiàn)Tcp通訊吧2023-12-12
詳解Unity中Mask和RectMask2D組件的對比與測試
本篇文章給大家介紹Unity中Mask和RectMask2D組件的對比與測試,包括組件用法及RectMask2D的基本用法,通過Mask的原理分析實例代碼相結(jié)合給大家講解的非常詳細,需要的朋友參考下吧2021-06-06
c# 線程定時器 System.Threading.Timer的使用
本文主要介紹了c# 線程定時器 System.Threading.Timer的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
適用于WebForm Mvc的Pager分頁組件C#實現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁組件,由C#實現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04
C#實現(xiàn)動態(tài)生成靜態(tài)頁面的類詳解
這篇文章主要介紹了C#實現(xiàn)動態(tài)生成靜態(tài)頁面的類,結(jié)合實例形式詳細分析了C#動態(tài)生成靜態(tài)頁面的原理與相關(guān)使用技巧,需要的朋友可以參考下2016-04-04

