C#設(shè)計模式之工廠模式
這是我們用得比較多的一種設(shè)計模式,也是23種標準設(shè)計模式之一,使用前面講的簡單工廠設(shè)計模式,遇到具體產(chǎn)品經(jīng)常變換時就不太適合了,違反了開閉設(shè)計原則;怎么才能避免修改工廠類呢?工廠方法模式可以做到。
工廠方法模式要求我們應(yīng)該有一個抽象的工廠類,我們知道盡量使用抽象類或接口來定義就可以達到一個開閉原則的效果,這樣我們在抽象的工廠類定義一個生產(chǎn)產(chǎn)品的方法,這個方法就是工廠方法,這也是工廠方法模式的由來,他具體的行為會有他的子類或?qū)崿F(xiàn)類來實現(xiàn)。如果想生產(chǎn)某種產(chǎn)品,就定義一個新的產(chǎn)品,新的產(chǎn)品工廠類,這樣就實現(xiàn)了不同的產(chǎn)品進行一個不同的創(chuàng)建,這樣如果有信的產(chǎn)品,只需要添加新的工廠類,原來寫好的代碼不會發(fā)生變化,這種方式符合開閉原則,可擴展比較好?!?/p>

添加一個具體產(chǎn)品,只需要在添加一個具體產(chǎn)品的工廠類實現(xiàn)抽象工廠類,不需要修改原來的代碼

示例代碼:
抽象產(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 工廠模式
{
/*
動物抽象工廠類
*/
public abstract class AnimalFactory
{
public abstract Animal GetAnimal();
}
}生產(chǎn)狗的具體工廠類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工廠模式
{
/// <summary>
/// 具體工廠:生成狗
/// </summary>
public class DogFactory :AnimalFactory
{
public override Animal GetAnimal()
{
return new Dog();
}
}
}生產(chǎn)企鵝的具體工廠類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工廠模式
{
/// <summary>
/// 具體工廠:生成企鵝
/// </summary>
public class PenguinFactory :AnimalFactory
{
public override Animal GetAnimal()
{
return new Penguin();
}
}
}具體產(chǎn)品狗類:
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("狗在吃飯!");
}
}
}具體產(chǎn)品企鵝類:
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("企鵝在吃飯!");
}
}
}客戶端調(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)
{
AnimalEat(new DogFactory());
Console.ReadKey();
}
static void AnimalEat(AnimalFactory af)
{
Animal am = af.GetAnimal();
am.Eat();
}
}
}類圖:

如果想在增加一個Cat類,只需要增加一個具體的Cat類實現(xiàn)Animal類的方法,增加一個具體的Cat工廠類實現(xiàn)抽象工廠類即可,不需要在修改已經(jīng)寫好的代碼,符合開閉原則。
使用接口的方式實現(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)里面的Work()方法
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、定義汽車的工廠接口(ICarFactory):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工廠模式
{
/// <summary>
/// 汽車工廠接口
/// </summary>
public interface ICarFactory
{
ICar GetCar();
}
}4、分別定義卡車工廠(TruckFactory)和公共汽車工廠(BusFactory)實現(xiàn)ICarFactory接口
TruckFactory工廠:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工廠模式
{
/// <summary>
/// 卡車工廠接口
/// </summary>
public class TruckFactory:ICarFactory
{
/// <summary>
/// 返回卡車類
/// </summary>
/// <returns></returns>
public ICar GetCar()
{
return new Truck();
}
}
}BusFactory工廠:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工廠模式
{
/// <summary>
/// 公共汽車工廠
/// </summary>
public class BusFactory:ICarFactory
{
/// <summary>
/// 返回公共汽車類
/// </summary>
/// <returns></returns>
public ICar GetCar()
{
return new Bus();
}
}
}5、主程序調(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)
{
CarWork(new TruckFactory());
Console.ReadKey();
}
/// <summary>
/// 根據(jù)汽車工廠返回具體的汽車類
/// </summary>
/// <param name="cf"></param>
static void CarWork(ICarFactory cf)
{
ICar c = cf.GetCar();
c.Work();
}
}
}6、程序運行結(jié)果

代碼下載地址:點擊下載
到此這篇關(guān)于C#設(shè)計模式之工廠模式的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Quartz.Net任務(wù)和觸發(fā)器實現(xiàn)方法詳解
這篇文章主要介紹了Quartz.Net任務(wù)和觸發(fā)器實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
C#實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標和開機自啟動
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標和開機自啟動功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-01-01
C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上
本文主要介紹用C#實現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下2015-08-08

