博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
工厂方法模式
阅读量:4704 次
发布时间:2019-06-10

本文共 2353 字,大约阅读时间需要 7 分钟。

故事:

  有个定制鞋子的工厂(还是那个工厂)(因为使用了设计模式应对了各种变化)效益比较好,决定把生产部门分成两个部门(休闲鞋部门/运动鞋部门)。每个部门生产相应的鞋子类型。

 

建模:

  工厂运动鞋生产部门/工厂休闲鞋生产部门。

  工厂前台接待处。

  鞋子。

  你还是这个工厂的客户。

 

类图:

 

实现:

HelpDesk

namespace FactoryMethod{    public class HelpDesk    {        ArrayList al = new ArrayList();        public HelpDesk()        {            SportsShoesDepartment ss = new SportsShoesDepartment();            al.Add(ss);            LeisureShoesDepartment ls = new LeisureShoesDepartment();            al.Add(ls);        }                public Shoes produceShoes(string shoesType)        {            Shoes shoes;            Console.WriteLine("
<工厂公告>
客户需要的鞋子类型是:{0}\n", shoesType); foreach(ShoesFactoryDepartment sfd in al) { shoes = sfd.produceShoes(shoesType); if (shoes != null) { return shoes; } } return null; } }}

ShoesFactoryDepartment

namespace FactoryMethod{    public abstract class ShoesFactoryDepartment    {        public abstract Shoes produceShoes(string shoesType);    }}

SportsShoesDepartment

namespace FactoryMethod{    public class SportsShoesDepartment: ShoesFactoryDepartment    {        public override Shoes produceShoes(string shoesType)        {            if (shoesType == "Sport")            {                return new SportShoes();            }            else            {                return null;            }        }    }}

LeisureShoesDepartment

namespace FactoryMethod{    public class LeisureShoesDepartment: ShoesFactoryDepartment    {        public override Shoes produceShoes(string shoesType)        {            if (shoesType == "Leisure")            {                return new LeisureShoes();            }            else            {                return null;            }        }    }}

Program

namespace FactoryMethod{    class Program    {        static void Main(string[] args)        {            HelpDesk helpDesk = new HelpDesk();            Console.WriteLine("请输入你想要的鞋子:Sport/Leisure");            string shoesType = Console.ReadLine();            Console.WriteLine("\n------进厂定制---------\n");            helpDesk.produceShoes(shoesType);            Console.WriteLine("\n-------鞋子出厂--------\n");                    }    }}

 

效果:

转载于:https://www.cnblogs.com/jiejue/archive/2012/10/04/2711632.html

你可能感兴趣的文章
用Hadoop构建电影推荐系统
查看>>
automake连载---关于两个文件configure.in和Makefile.am的编写
查看>>
JQuery选择器中含有冒号的ID处理差异的分析
查看>>
分享:一款前端布局工具(alloydesigner)
查看>>
python模拟老师授课下课情景
查看>>
C# 定积分求周长&面积原理 代码实现
查看>>
freemarker测试
查看>>
外观模式
查看>>
C# Thread IsAlive 理解
查看>>
grep 详解
查看>>
安装完 MySQL 后必须调整的 10 项配置
查看>>
51学习笔记之关键词data、bdata、idata、pdata、xdata、code的含义
查看>>
rc.local中的memcached命令不执行
查看>>
分布式文件管理系统MFS
查看>>
前端开发本地存储之cookie
查看>>
Android 检测网络连接状态
查看>>
快速排序
查看>>
条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
查看>>
如何学习页面重构/对页面重构理解
查看>>
Mongodb 启动关闭脚本并设置开机自动启动Mongodb
查看>>