工厂方法模式
来源:互联网 发布:美丽说网络兼职客服 编辑:程序博客网 时间:2024/06/14 19:43
有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更多的品牌笔记本电脑的订单Acer,Lenovo,Dell,该OEM商发现,如果一次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,用C#控制台应用程序实现该OEM制造商的工厂模式。绘制该模式的UML图。
public abstract class Laptop { public abstract void show(); } public class HPLaptop:Laptop { public override void show() { Console.WriteLine("惠普笔记本"); } } public class AcerLaptop : Laptop { public override void show() { Console.WriteLine("Acer"); } } public class LenovoLaptop : Laptop { public override void show() { Console.WriteLine("Lenovo"); } } public class DellLaptop : Laptop { public override void show() { Console.WriteLine("Dell"); } } interface IFactory { Laptop CreateLaptop(); } class HPFactory : IFactory { public Laptop CreateLaptop() { return new HPLaptop(); } } class AcerFactory : IFactory { public Laptop CreateLaptop() { return new AcerLaptop(); } } class LenovoFactory : IFactory { public Laptop CreateLaptop() { return new LenovoLaptop(); } } class DellFactory : IFactory { public Laptop CreateLaptop() { return new DellLaptop(); } } class Program { static void Main(string[] args) { IFactory lf = new HPFactory(); Laptop tp = lf.CreateLaptop(); tp.show(); lf = new AcerFactory(); tp = lf.CreateLaptop(); tp.show(); lf = new LenovoFactory(); tp = lf.CreateLaptop(); tp.show(); lf = new DellFactory(); tp = lf.CreateLaptop(); tp.show(); }}
阅读全文
0 0
- 工厂模式 -- 工厂方法
- 工厂模式-工厂方法
- 工厂模式-工厂方法
- 工厂 > 工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式---工厂方法模式
- 【模式】工厂方法模式
- 工厂方法,抽象工厂模式
- C#工厂模式-工厂方法
- 工厂模式 和工厂方法
- 工厂模式-C-工厂方法
- 工厂模式以及工厂方法
- 简单工厂、工厂方法、抽象工厂模式
- 简单工厂、工厂方法、抽象工厂模式
- 工厂模式(简单工厂、工厂方法)
- 简单工厂|工厂方法|抽象工厂模式
- 设计模式-工厂模式(简单工厂模式+工厂方法模式)
- 最全三大框架整合(使用映射)——index.jsp
- Hadoop-rpc调用案例,服务端,客户端代码案例
- Vladik and Favorite Game CodeForces
- 快速提高编程能力---一年时间可以改变很多事
- iOS小demo之二阶贝塞尔画线
- 工厂方法模式
- 20170531_动态优化的一个小例子
- leetcode 199. Binary Tree Right Side View
- 省赛 Binary to Prime
- UR5机械臂仿真环境搭建
- 以小米时钟为demo学习自定义view过程总结
- 选择器
- 对机器学习的理解
- Unity3D 原生WebCamera实现摄像头显示