發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
四個組成部分:抽象產(chǎn)品接口、具體產(chǎn)品、抽象工廠接口、具體工廠抽象工廠可以包含不同的工廠,不同的工廠可以創(chuàng)建出相通目標(biāo)類的不同實(shí)例
interface IProductA { void ProductA(); }
interface IProductB { void ProductB(); }
class ProductAOne:IProductA { public void ProductA() { Console.WriteLine("Products A--One"); } }
class ProductATwo:IProductA { public void ProductA() { Console.WriteLine("Products A--Two"); } }
class ProductBOne:IProductB { public void ProductB() { Console.WriteLine("Products B--One"); } }
class ProductBTwo:IProductB { public void ProductB() { Console.WriteLine("Products B--Two"); } }
interface IFactory { IProductA CreateProductA(); IProductB CreateProductB(); }
class FactoryOne:IFactory { public IProductA CreateProductA() { return new ProductAOne(); } public IProductB CreateProductB() { return new ProductBOne(); } }
class FactoryTwo:IFactory { public IProductA CreateProductA() { return new ProductATwo(); } public IProductB CreateProductB() { return new ProductBTwo(); } }
static void Main(string[] args) { var factory = new FactoryOne(); var productAOne = factory.CreateProductA(); productAOne.ProductA(); var productBOne = factory.CreateProductB(); productBOne.ProductB(); var newfactory = new FactoryTwo(); var productATwo = newfactory.CreateProductA(); productATwo.ProductA(); var productBTwo = newfactory.CreateProductB(); productBTwo.ProductB(); Console.ReadKey(); }
來自: 昵稱10504424 > 《工作》
0條評論
發(fā)表
請遵守用戶 評論公約
設(shè)計(jì)模式 2/23 工廠模式
所有的處理類已經(jīng)Ready了,我們的重點(diǎn)工廠可以開始修建了 public class HandleFactory { public Handle CreateHandle(string fileType) { Handle handle = null; switch (fileType) { case ''V...
PHP設(shè)計(jì)模式之工廠方法模式
PHP設(shè)計(jì)模式之工廠方法模式PHP設(shè)計(jì)模式之工廠方法模式。工廠方法模式對比簡單工廠來說,最核心的一點(diǎn),其實(shí)就是將實(shí)現(xiàn)推遲到子類。沒錯...
設(shè)計(jì)模式之工廠模式(三)
設(shè)計(jì)模式之工廠模式(三)在抽象的Creator中,任何其他實(shí)現(xiàn)的方法,都可能使用到這個工廠方法所制造出來的產(chǎn)品,但只有子類真正實(shí)現(xiàn)這個...
設(shè)計(jì)模式之享元模式
設(shè)計(jì)模式之享元模式享元模式 FlyweightIntro.}public class ConcreteFlyweight : Flyweight{ public override void Operation(int extrinsicstate) { Console.}}public class FlyWeightFactory{ privat...
設(shè)計(jì)模式中的那些工廠
設(shè)計(jì)模式中有幾個工廠模式,聊一聊這幾個工廠模式的各自用法和使用示例,工廠模式包含簡單工廠,抽象工廠,工廠方法,這些均屬于創(chuàng)建型模式,所謂創(chuàng)建型模式,就是說這幾個設(shè)計(jì)模式是用來創(chuàng)建對象的。...
前端5大常見設(shè)計(jì)模式、代碼一看你就懂!
工廠模式是用來創(chuàng)建對象的一種最常用的設(shè)計(jì)模式,不暴露創(chuàng)建對象的具體邏輯,而是將將邏輯封裝在一個函數(shù)中,那么這個函數(shù)就可以被視為一個工廠,工廠模式根據(jù)抽象程度的不同可以分為:簡單工廠,工廠...
深入學(xué)習(xí)《大話設(shè)計(jì)模式》 簡單工廠模式
/// <summary> /// 數(shù)據(jù)庫工廠 /// </summary> public class DBFactory { public static ConnetionAbstract CreateConnetion(DBConnetionEnum dBConnetionEnum) { ConnetionAbstract conne...
設(shè)計(jì)模式圖解學(xué)習(xí)(23種)
設(shè)計(jì)模式分類
三、行為型模式 模版方法模式(Template Method) 命令模式(Command Pattern) 迭代器模式(Iterator Pattern) 觀察者模式(Oberver Pattern) 中介者模式(Mediator Pattern) 備...
微信掃碼,在手機(jī)上查看選中內(nèi)容