2.工厂方法模式
来源:互联网 发布:golang microservice 编辑:程序博客网 时间:2024/05/16 16:22
工程方法模式和简单工程合并成同一个模式,所以标题都用2了。。。不要在意这些细节
介绍
简单工厂模式的缺点很明显,简单工厂模式系统难以扩展,一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过于复杂,工厂方法模式可以解决简单工厂模式中存在的这个问题。
核心要点
“一个工场只能生产单个东西。”工厂接口,实现不同的工厂把具体产品的创建推迟到子类中,此时工厂类不再负责所有产品的创建,而只是给出具体工厂必须实现的接口,这样工厂方法模式就可以允许系统不修改工厂类逻辑的情况下来添加新产品
例子
class UIElement{ public string Name { get; set; }} interface ILegendFactory{ UIElement Create();}class LegendFactory:ILegendFactory{ public UIElement Create() { return new UIElement() { Name = "Legend" }; }}class TitleFactory:ILegendFactory{ public UIElement Create() { return new UIElement() { Name = "Title" }; }} class SymbolFactory:ILegendFactory{ public UIElement Create() { return new UIElement() { Name="Smybol"}; }}//使用class Program{ static void Main(string[] args) { ILegendFactory factory = new SymbolFactory(); Console.WriteLine( factory.Create().Name); Console.ReadKey(); }}
阅读全文
0 0
- 2.工厂方法模式
- 2.工厂方法模式
- 2.工厂方法模式
- 2.工厂方法模式
- 2.工厂方法模式
- 2.工厂模式(简单工厂模式,工厂方法模式,抽象工厂模式)(设计模式笔记)
- 工厂模式 -- 工厂方法
- 工厂模式-工厂方法
- 工厂模式-工厂方法
- 工厂 > 工厂方法模式
- 2.FactoryMethod-工厂方法模式
- 2.FactoryMethod-工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式---工厂方法模式
- 深入设计模式-2.工厂方法模式
- 【模式】工厂方法模式
- 工厂方法,抽象工厂模式
- 商业模式有哪些?
- 网页设计与制作(HTML+CSS)(二)
- java压缩解压文件(代码记录)
- 高性能mysql笔记(七)高性能的索引策略
- 杭电OJ--1.2.3--Climbing Worm
- 2.工厂方法模式
- Android中getResources().getDrawable() 过时的解决方法
- 3.抽象工厂模式
- BZOJ 4894(天赋-有向图生成树计数)
- Linux进程环境之进程虚拟空间及存储
- 批量恢复 Mac OS X 废纸篓里的文件
- 半透明AlphaBlend
- 模版引擎详解
- 541. Reverse String II