C#设计模式之工厂
IronMan之工厂
前言
实用为主,学一些用得到的技术更能在如今的社会里保命。 虽然在日常的工作中设计模式不是经常的用到,但是呢,学习它只有好处没有坏处。
设计模式像是一种“标签”,它是代码编写者思想的体现。有木有感觉到这样很便捷?看到一些代码的时候就很清楚的了解编写者的思想了,这是为什么呢?因为编写者们用了“标签”,而你恰恰是知道“标签”意思的。 跟一异曲同工,在学习框架、了解框架的对象模型的时候,“标签”是时常出现,如果你不了解“标签”的意思那多痛苦啊!!!!! 还有好多,不去一一阐述了。
工作中需求的变更时常令我们头疼不已,总是要按着需求来重新的修改代码,然后发布新的产品版本,或者是出更新包。 在需求变更中,对应着代码也要修改,但是这修改也是分层次的。比如修改的模板在当初设计的时候遵循了开闭原则(OCP)的话,代码的修改就变的轻松的多了。
我想制造出一个像电影《钢铁侠》里面那样的的一身盔甲,又或者说是机器吧,我把这个想法告诉了我的朋友们,他们都认为我疯了。
好吧,我说的是用代码抽象的制造出”钢铁侠“
废话不多说,下面这个系列是用钢铁侠(IronMan)作为主题的设计模式 今天来学习简单工厂模式、工厂方法模式、以及抽象工厂模式。
问题的发现
需求:
“玩具厂”有一天找到我说:“Jin叫兽我们这需要一些部件,我们会提供“图纸”,请您帮忙制造,您看有问题吗?”。
我:“当然没有问题了。很乐意帮助你们”。
“玩具厂”:“噢!好的。Jin叫兽,时间方面还有什么问题嘛?”
我:“没问题的,我会尽快的”。
“玩具厂”:“那真的太感谢您了,Jin叫兽。我们就不打扰您了,先走了。”
我:“晚上一起吃顿饭吧”。
“玩具厂”:“Jin叫兽不必客气啊”。
我:“好,你们慢走”。
生产车间:
“一点挑战性都没有,随随便便就制造出来了。先从组成‘钢铁侠’的部件开始生产吧。”
既然是这样,那就从部件开始生产吧。
这里是“玩具厂”提供的“钢铁侠”右臂的“图纸”:
public class RightHandComponent
{
public RightHandComponent()
{
this.strName = "毅代先锋号一代右部件"; }
public RightHandComponent(string strname)
{
this.strName = strname;
}
private string strName = string.Empty;
public string Name
{
get { return strName; }
set { strName = value; }
}
}
还有一个左臂部件的“图纸”:
public class LeftHandComponent
{
public LeftHandComponent()
{
this.strName = "毅代先锋号一代左部件";
}
private string strName = string.Empty;
public string Name
{
get { return strName; }
set { strName = value; }
}
}
还有若干“图纸”…… 不在这篇一一列举了,在后面文章中会全面的讲解到。
开始生产
RightHandComponent rightcomponent = new RightHandComponent();
LeftHandComponent leftcomponent = new LeftHandComponent();
若干的部件
被我一个个的按照图纸给制造了出来。
这样就可以拿rightcomponent、leftcomponent……等等的一些制造好的部件组装了。
我也可以给“玩具厂”打电话了,可以交货收钱。可是电话打完情况又变了,
中间的对话就不说了,“玩具厂”的意思就是这样的。比如说“运输途中”出现“损坏”,他们希望用一个很快捷便利的方式能得到部件。
简单工厂
嗯,我想了一下,不过对于我Jin叫兽来说都是小事,待我造出来一台部件生产器来。他们拿着机器自己回家生产不就ok了。
public class IronManComponentFactory
{
public static object CreateComponent(string comname)
{
switch (comname)
{
case "RightCom":
return new RightHandComponent();
case "LeftCom":
return new LeftHandComponent(); 等等……
}
return null;
}
}
这样生产的“机器”就好了,我得自己试用一下,看看效果怎么样。
RightHandComponent rightcomponent = IronManComponentFactory.CreateComponent("RightCom") as RightHandComponent;
LeftHandComponent leftcomponent = IronManComponentFactory.CreateComponent("LeftCom") as LeftHandComponent;
这样反而多此一举了,第一步,向机器输入了命令以便自己获得想要的部件,第二步,在生产出来后我还得把“图纸”拿过来比对一下。 在我一筹莫展的时候,“玩具厂”给我发了个邮件,给我发了一份“部件规范说明书”,要求我的机器生产出的部件是达到说明书标准的。
那我们就来看一下这个所谓的“部件规范说明书”
public abstract class Component
{
private string strName = string.Empty;
/// <summary>
/// 名称
/// </summary>
public string Name
{
get { return strName; }
set { strName = value; }
}
/// <summary>
/// 自我描述
/// </summary>
public abstract void Self_Described();
}
看完这个“部件规范说明书”,我已经无力吐槽,没办法了,只有重新设计部件了,“图纸”要重新绘制,因为要按照那该死的“规范说明书”。
再看一下各个部件的“图纸”
public class RightHandComponent:Component
{
public RightHandComponent()
{
base.Name = "毅代先锋号一代右部件";
//fWeight=材质输出
}
public RightHandComponent(string strname)
{
base.Name = strname;
}
public override void Self_Described()
{
Console.WriteLine(base.Name);
}
}
public class LeftHandComponent:Component
{
public LeftHandComponent()
{
base.Name = "毅代先锋号一代左部件";
}
public LeftHandComponent(string strname)
{
base.Name = strname;
}
public override void Self_Described()
{
Console.WriteLine(base.Name);
}
}
等等一些部件……
这下我再把原来的搭好的“生产机器”拆了,内部修改一下,来看一下修改后的“机器”
public class IronManComponentFactory
{
public static Component CreateComponent(string comname)
{
switch (comname)
{
case "RightCom":
return new RightHandComponent();
case "LeftCom":
return new LeftHandComponent(); }
return null;
}
}
自己来测试一下:
Component rightcomponent = IronManComponentFactory.CreateComponent("RightCom");
Component leftcomponent = IronManComponentFactory.CreateComponent("LeftCom");
rightcomponent.Self_Described();
leftcomponent.Self_Described();
于是,我很放心的就交给了“玩具厂”使用了,还得了个好评。
工厂方法
好景不长,“玩具厂”再次来电,提出一个要求,想让我的“生产机器”里面还可以生产更多的符合“部件规范说明书”的部件。
这样说来是要改装“生产机器”了。 可以说“玩具厂”是故意的,绝对是故意的。这下整蒙圈了,亏血本了,“生产机器”又要拆了…… 直接来看“生产机器”的图纸
public abstract class IronManComponentFactory
{
protected string factoryName = string.Empty;
protected void InitDataMessage()
{
Console.WriteLine("生产机器:" + factoryName + ".准备就绪");
}
public abstract Component CreateComponent();
}
public class RightComFactory:IronManComponentFactory
{ public override Component CreateComponent()
{
base.factoryName = "右臂部件生产器";
InitDataMessage();
return new RightHandComponent();
}
}
public class LeftComFactory:IronManComponentFactory
{ public override Component CreateComponent()
{
base.factoryName = "左臂部件生产器";
InitDataMessage();
return new LeftHandComponent();
}
}
public class PottingFactrory
{
/// <summary>
/// 取件口
/// </summary>
/// <param name="comFactory"></param>
/// <returns></returns>
public static Component ComExit(IronManComponentFactory comFactory)
{
if (comFactory != null)
{
return comFactory.CreateComponent();
}
return null;
}
}

我们通过“取件口”取件
Component component = PottingFactrory.ComExit(new RightComFactory());
component.Self_Described();

我们只要通过更换“生产插件”,从“取件口”获得不同的部件。 或者更改需求,又要生产符合“规范说明书”的另一些产品的时候,可以自定义来实现“生产插件”,插入“生产机器”即可。
这样可以送给“玩具厂”使用了。这次得到了很好的一个评价,我也可以休息一下了。
这里的工厂方法是开闭原则(OCP)的实现,很优雅的显示出了设计原则和设计模式的美。
抽象工厂
“对,没错。‘玩具厂’又来了……。”
这次“玩具厂”是把Iron部件升级了,二代部件出来了,是在一代的基础上做了一些个性化的设计。来看新增部件的“图纸”
public class RightHandComponent2 : Component
{
public RightHandComponent2() : this("毅代先锋号二代右部件") { }
public RightHandComponent2(string strname)
{
base.Name = strname;
}
public override void Self_Described()
{
Console.WriteLine("自描述:我是->" + base.Name);
}
} public class LeftHandComponent2 : Component
{
public LeftHandComponent2() : this("毅代先锋号一代左部件") { }
public LeftHandComponent2(string strname)
{
base.Name = strname;
}
public override void Self_Described()
{
Console.WriteLine("自描述:我是->" + base.Name);
}
}
按照“规范说明书”,添加了两张“图纸”,它们分别是二代的左右手部件。
部件的整体结构没怎么变,但是“生产机器”的“规范说明”就要更改了,以为在上面说到的“生产插件”也要重新更改。
先来看一下“生产机器”的“规范说明”:
public abstract class IronManComponentFactory
{
protected string factoryName = string.Empty;
protected void InitDataMessage()
{
Console.WriteLine("生产机器:" + factoryName + ".准备就绪");
}
public abstract Component CreateComponent();
public abstract Component CreateComponent2(); }
当然喽,“规范说明”都改了,那上面说过的两个生产插件也得改了:
public class RightComFactory : IronManComponentFactory //右部件生产插件
{ public override Component CreateComponent()
{
base.factoryName = "新右臂部件生产器";
InitDataMessage();
return new RightHandComponent();
} public override Component CreateComponent2()
{
base.factoryName = "新右臂部件生产器";
InitDataMessage();
return new RightHandComponent2();
}
} public class LeftComFactory : IronManComponentFactory //左部件生产插件
{ public override Component CreateComponent()
{
base.factoryName = "新左臂部件生产器";
InitDataMessage();
return new LeftHandComponent();
} public override Component CreateComponent2()
{
base.factoryName = "新左臂部件生产器";
InitDataMessage();
return new LeftHandComponent2();
}
}
所要做的修改都是连带关系,包装”生产机器“的”取件口“也要改:
public class PottingFactory
{
private IronManComponentFactory comFactory = null; public IronManComponentFactory ComFactory
{
get { return comFactory; }
set { comFactory = value; }
}
public PottingFactory(IronManComponentFactory comFactory)
{
this.comFactory = comFactory;
}
/// <summary>
/// 取件口
/// </summary>
/// <param name="comFactory"></param>
/// <returns></returns>
//public static Component ComExit(IronManComponentFactory comFactory)
//{
// if (comFactory != null)
// {
// return comFactory.CreateComponent();
// }
// return null;
//}
}
到这里就差不多结束了。在工厂方法模式上 做了稍稍的(几乎全改了)改动。
当然自己要先来测试一下喽:
PottingFactory pottingfact = new PottingFactory(new RightComFactory());
Component component= pottingfact.ComFactory.CreateComponent();
component.Self_Described();
component = pottingfact.ComFactory.CreateComponent2();
component.Self_Described();
我们来看一下结果:

End IronMan之旅才刚刚开始
C#设计模式之工厂的更多相关文章
- C#设计模式(3)——工厂方法模式
一.概念:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 二.代码实现 namespace 设计模式之工厂方法模式 { /// <summary&g ...
- Java设计模式之工厂模式(Factory)
前言: 前面学习了建造者设计模式,接下来学习一下Retrofit中使用的另外一个设计模式,工厂设计模式!!!里面采用工厂模式使得数据转换得到完全解耦,工厂模式的好处用到了极致,如此好的设计模式我们怎能 ...
- 设计模式——抽象工厂模式及java实现
设计模式--抽象工厂模式及java实现 设计模式在大型软件工程中很重要,软件工程中采用了优秀的设计模式有利于代码维护,方便日后更改和添加功能. 设计模式有很多,而且也随着时间在不断增多,其中最著名的是 ...
- java 23 - 1 设计模式之工厂方法模式
转载: JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- 5. 星际争霸之php设计模式--抽象工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 3. 星际争霸之php设计模式--简单工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- 设计模式之工厂模式(Factory)
设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...
- php设计模式:工厂模式
php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
随机推荐
- to_char函数
TO_CHAR 是把日期或数字转换为字符串,不能指定字符串长度. 使用TO_CHAR函数处理日期:TO_CHAR(number, '格式') eg:TO_CHAR(salary,'$99,999.99 ...
- SQL SERVER 得到汉字首字母函数四版全集 --【叶子】
--创建取汉字首字母函数(第三版) create function [dbo].[f_getpy_V3] ( ) ) ) as begin ),) ,@len = len(@col),@sql = ' ...
- HTML导航栏
先看效果(两种,1:自己写样式,写交互,2.用jQueryUI 的menu),如下图 第一种: 第二种: 第一种样式: 然后就开始准备了,单村用js和css也可以写出来,不过既然有jq ...
- TotoiseSVN的基本使用方法
TotoiseSVN的基本使用方法 在 项目管理实践教程一.工欲善其事,必先利其器[Basic Tools]中,我已经讲解了怎样安装TortoiseSVN.在上面的讲解中已经讲了怎么使用VisualS ...
- nodejs复习01
console 格式化 console.log("%s:%s", "a", "b") //字符串 console.log("%d. ...
- Java对象大小计算
这篇说说如何计算Java对象大小的方法.之前在聊聊高并发(四)Java对象的表示模型和运行时内存表示 这篇中已经说了Java对象的内存表示模型是Oop-Klass模型. 普通对象的结构如下,按64位机 ...
- 【转】Java提高篇(三四)-----fail-fast机制
转自:http://blog.csdn.net/chenssy/article/details/38151189 在JDK的Collection中我们时常会看到类似于这样的话: 例如 ...
- 洛谷 P1827 美国血统 American Heritage Label:字符串Water
题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树的中序遍历”和“树的前序遍历”的符号加以记录而 不是用图形 ...
- 德国W家HIPP 奶粉有货播报:2014.7.8 HIPP 奶粉 1+ 4盒装有货啦!
德国W家HIPP 奶粉有货播报:2014.7.8 HIPP 奶粉 1+ 4盒装有货啦!
- 我的前端故事----优美的编辑器GitHub Atom
很多前端的同学都在用sublime text,我之前也在使用,但是后来接触到了Atom,就被它的高颜值深深的吸引了~~不愧是GitHub的工程师哦~审美就是高 Atom 作为一个跨平台的编辑软件,安 ...