设计模式——工厂方法 FactoryMethod
2016-07-18 22:33
363 查看
工厂接口
手机基类
三星手机
三星手机工厂
苹果手机
苹果手机工厂
魅族手机
魅族手机工厂
上端调用
上面可以看出,只要我们在增加一个手机类型,同时在增加一个该类型手机工厂,那么我们对业务就可以很好的扩展
设计模式第6个原则——开闭原则。
开闭原则:对扩展开发,对修改封闭
在某种角度上来说工厂方法是最完美的设计模式,因为它完美的遵循了开闭原则
工厂方法使用起来太麻烦,要多很多类和工厂,感觉是类的大爆炸 :(
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { /// <summary> /// 手机工厂接口 /// </summary> public interface IBasePhoneFactory { BasePhone CreateBasePhone(); } }
手机基类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Interface { public abstract class BasePhone { public abstract void Call(); public abstract void Text(); } }
三星手机
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class Galaxy : BasePhone { public Galaxy(string name) { } public override void Call() { Console.WriteLine("use {0} call", this.GetType().Name); } public override void Text() { Console.WriteLine("use {0} text", this.GetType().Name); } } }
三星手机工厂
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class GalaxyFactory : IBasePhoneFactory { public BasePhone CreateBasePhone() { return new Galaxy("FY"); } } }
苹果手机
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class iPhone : BasePhone { public override void Call() { Console.WriteLine("use {0} call", this.GetType().Name); } public override void Text() { Console.WriteLine("use {0} text", this.GetType().Name); } } }
苹果手机工厂
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class iPhoneFactory : IBasePhoneFactory { public BasePhone CreateBasePhone() { return new iPhone(); } } }
魅族手机
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class MX : BasePhone { public override void Call() { Console.WriteLine("use {0} call", this.GetType().Name); } public override void Text() { Console.WriteLine("use {0} text", this.GetType().Name); } } }
魅族手机工厂
using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class MXFactory : IBasePhoneFactory { public BasePhone CreateBasePhone() { return new MX(); } } }
上端调用
using FactoryMethod; using Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { BasePhone galaxy = new GalaxyFactory().CreateBasePhone(); BasePhone iPhone = new iPhoneFactory().CreateBasePhone(); BasePhone MX = new MXFactory().CreateBasePhone(); } } }
上面可以看出,只要我们在增加一个手机类型,同时在增加一个该类型手机工厂,那么我们对业务就可以很好的扩展
设计模式第6个原则——开闭原则。
开闭原则:对扩展开发,对修改封闭
在某种角度上来说工厂方法是最完美的设计模式,因为它完美的遵循了开闭原则
工厂方法使用起来太麻烦,要多很多类和工厂,感觉是类的大爆炸 :(
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C# 设计模式系列教程-建造者模式
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用
- 详解C#设计模式编程中生成器模式的使用