发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
四个组成部分:抽象产品接口、具体产品、抽象工厂接口、具体工厂抽象工厂可以包含不同的工厂,不同的工厂可以创建出相通目标类的不同实例
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条评论
发表
请遵守用户 评论公约
设计模式 2/23 工厂模式
所有的处理类已经Ready了,我们的重点工厂可以开始修建了 public class HandleFactory { public Handle CreateHandle(string fileType) { Handle handle = null; switch (fileType) { case ''V...
PHP设计模式之工厂方法模式
PHP设计模式之工厂方法模式PHP设计模式之工厂方法模式。工厂方法模式对比简单工厂来说,最核心的一点,其实就是将实现推迟到子类。没错...
设计模式之工厂模式(三)
设计模式之工厂模式(三)在抽象的Creator中,任何其他实现的方法,都可能使用到这个工厂方法所制造出来的产品,但只有子类真正实现这个...
设计模式之享元模式
设计模式之享元模式享元模式 FlyweightIntro.}public class ConcreteFlyweight : Flyweight{ public override void Operation(int extrinsicstate) { Console.}}public class FlyWeightFactory{ privat...
设计模式中的那些工厂
设计模式中有几个工厂模式,聊一聊这几个工厂模式的各自用法和使用示例,工厂模式包含简单工厂,抽象工厂,工厂方法,这些均属于创建型模式,所谓创建型模式,就是说这几个设计模式是用来创建对象的。...
前端5大常见设计模式、代码一看你就懂!
工厂模式是用来创建对象的一种最常用的设计模式,不暴露创建对象的具体逻辑,而是将将逻辑封装在一个函数中,那么这个函数就可以被视为一个工厂,工厂模式根据抽象程度的不同可以分为:简单工厂,工厂...
深入学习《大话设计模式》 简单工厂模式
/// <summary> /// 数据库工厂 /// </summary> public class DBFactory { public static ConnetionAbstract CreateConnetion(DBConnetionEnum dBConnetionEnum) { ConnetionAbstract conne...
设计模式图解学习(23种)
设计模式分类
三、行为型模式 模版方法模式(Template Method) 命令模式(Command Pattern) 迭代器模式(Iterator Pattern) 观察者模式(Oberver Pattern) 中介者模式(Mediator Pattern) 备...
微信扫码,在手机上查看选中内容