设计模式——工厂方法 FactoryMethod
来源:互联网 发布:mt4软件 编辑:程序博客网 时间:2024/05/29 17:52
工厂接口
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个原则——开闭原则。
开闭原则:对扩展开发,对修改封闭
在某种角度上来说工厂方法是最完美的设计模式,因为它完美的遵循了开闭原则
工厂方法使用起来太麻烦,要多很多类和工厂,感觉是类的大爆炸 :(
0 0
- 设计模式——工厂方法 FactoryMethod
- 【设计模式】——工厂方法FactoryMethod
- 设计模式之—工厂方法模式(FactoryMethod)-Java实现
- 浅谈JAVA设计模式之——工厂方法(FactoryMethod)
- 设计模式--工厂方法(FactoryMethod)
- 设计模式工厂方法(FactoryMethod)
- 设计模式—FactoryMethod工厂模式
- 设计模式——工厂方法模式(FactoryMethod)和抽象工厂模式(Abstract Factory)
- 设计模式之--工厂方法模式(FactoryMethod )
- 设计模式 -- 工厂方法模式 FactoryMethod
- 设计模式之工厂方法模式(FactoryMethod)
- 设计模式-工厂方法模式-factorymethod-python
- 设计模式之FactoryMethod(工厂方法)
- 设计模式(二) 工厂方法(FactoryMethod)
- 23设计模式之工厂方法(FactoryMethod)
- 23设计模式之工厂方法(FactoryMethod)
- 23种设计模式-FactoryMethod工厂方法
- FactoryMethod 工厂方法模式
- Android——RuntimePermission介绍
- 信息安全基础虚拟机实验的一些问题及解决(win2003)
- 给groupBox添加滚动条
- Permutations
- 菲波那切数列
- 设计模式——工厂方法 FactoryMethod
- java和scala实现oracle lpad\rpad函数
- WPF中DataGrid数据绑定(泛型和datatable)
- hibernate学习 hibernate-tutorials(三)——entitymanager
- 【翻译】LearnYouSomeErlangForGreatGood(三):真·起始
- 一个程序员的顿悟:理想的程序员只比你多了6个一点点
- 判断一棵二叉树是否为完全二叉树
- 软件开发文档流程,指导怎样从那些方面构建一个完善的软件使用指南
- Android开发中的Handler总结