<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>思维 &#187; 简单</title>
	<atom:link href="http://blog.acmind.com/archives/tag/%e7%ae%80%e5%8d%95/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.acmind.com</link>
	<description>Acme of Mind</description>
	<lastBuildDate>Mon, 19 Apr 2010 02:23:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>简单工厂模式VS.工厂方法模式</title>
		<link>http://blog.acmind.com/archives/126</link>
		<comments>http://blog.acmind.com/archives/126#comments</comments>
		<pubDate>Sat, 17 Jan 2009 01:58:09 +0000</pubDate>
		<dc:creator>笑谈</dc:creator>
				<category><![CDATA[架构分析]]></category>
		<category><![CDATA[工厂]]></category>
		<category><![CDATA[简单]]></category>
		<category><![CDATA[设计技巧]]></category>
		<category><![CDATA[设计模式]]></category>
		<category><![CDATA[抽象]]></category>
		<category><![CDATA[模式]]></category>

		<guid isPermaLink="false">http://blog.acmind.com/?p=126</guid>
		<description><![CDATA[先将上面2个图拿出来做比较：
简单工厂：

工厂方法：

1、简单工厂模式下，工厂类与具体父类及具体子类都有关联；而工厂模式下，工厂类只与具体父类有关联，由工厂子类负责与具体子类进行关联。
2、如果在简单工厂模式下加入新功能扩展，就要对工厂类做修改，加入新功能；而在工厂模式下，只是单纯地加入工厂子类和具体子类，而不需要对工厂类做修改，做到了“开放扩展，封闭修改”。
3、其实尽管对工厂类不需要做修改了，但是也要对客户端做修改，因为添加了对新功能的调用。也就是说工厂模式做到了对工厂类的开闭原则，却解决不了对客户端的修改。
4、一些情况下，可以考虑用工厂模式＋反射来解决客户端修改问题。
]]></description>
			<content:encoded><![CDATA[<p>先将上面2个图拿出来做比较：</p>
<p>简单工厂：</p>
<p><a href="http://blog.acmind.com/wp-content/uploads/2009/01/clip-image0011.jpg" class="highslide-image" onclick="return hs.expand(this);"><img title="clip_image001" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="343" alt="clip_image001" src="http://blog.acmind.com/wp-content/uploads/2009/01/clip-image001-thumb1.jpg" width="531" border="0" /></a></p>
<p>工厂方法：</p>
<p><a href="http://blog.acmind.com/wp-content/uploads/2009/01/clip-image0021.jpg" class="highslide-image" onclick="return hs.expand(this);"><img title="clip_image002" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="389" alt="clip_image002" src="http://blog.acmind.com/wp-content/uploads/2009/01/clip-image002-thumb2.jpg" width="673" border="0" /></a></p>
<p>1、简单工厂模式下，工厂类与具体父类及具体子类都有关联；而工厂模式下，工厂类只与具体父类有关联，由工厂子类负责与具体子类进行关联。</p>
<p>2、如果在简单工厂模式下加入新功能扩展，就要对工厂类做修改，加入新功能；而在工厂模式下，只是单纯地加入工厂子类和具体子类，而不需要对工厂类做修改，做到了“开放扩展，封闭修改”。</p>
<p>3、其实尽管对工厂类不需要做修改了，但是也要对客户端做修改，因为添加了对新功能的调用。也就是说工厂模式做到了对工厂类的开闭原则，却解决不了对客户端的修改。</p>
<p>4、一些情况下，可以考虑用工厂模式＋反射来解决客户端修改问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.acmind.com/archives/126/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>简单工厂模式</title>
		<link>http://blog.acmind.com/archives/94</link>
		<comments>http://blog.acmind.com/archives/94#comments</comments>
		<pubDate>Fri, 16 Jan 2009 18:06:39 +0000</pubDate>
		<dc:creator>笑谈</dc:creator>
				<category><![CDATA[架构分析]]></category>
		<category><![CDATA[工厂]]></category>
		<category><![CDATA[简单]]></category>
		<category><![CDATA[设计技巧]]></category>
		<category><![CDATA[设计模式]]></category>
		<category><![CDATA[模式]]></category>

		<guid isPermaLink="false">http://blog.acmind.com/?p=94</guid>
		<description><![CDATA[&#160;
Operation.cs
public class Operation
{
    private double _numberA = 0;
    private double _numberB = 0;

    public double NumberA
    {
        get { return _numberA; }
        set { _numberA = value; }
  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.acmind.com/wp-content/uploads/2009/01/image3.png" class="highslide-image" onclick="return hs.expand(this);"><img title="" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="347" alt="" src="http://blog.acmind.com/wp-content/uploads/2009/01/image-thumb3.png" width="535" border="0" /></a>&#160;
<p>Operation.cs</p>
<pre class="mycode"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Operation
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span> _numberA = 0;
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span> _numberB = 0;

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> NumberA
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _numberA; }
        <span style="color: #0000ff">set</span> { _numberA = <span style="color: #0000ff">value</span>; }
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> NumberB
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _numberB; }
        <span style="color: #0000ff">set</span> { _numberB = <span style="color: #0000ff">value</span>; }
    }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">double</span> GetResult()
    {
        <span style="color: #0000ff">double</span> result = 0;
        <span style="color: #0000ff">return</span> result;
    }
}</pre>
<p>OperationAdd.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> OperationAdd : Operation
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">double</span> GetResult()
    {
        <span style="color: #0000ff">double</span> result = 0;
        result = NumberA + NumberB;
        <span style="color: #0000ff">return</span> result;
    }
}</pre>
<p>OperationSub.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> OperationSub : Operation
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">double</span> GetResult()
    {
        <span style="color: #0000ff">double</span> result = 0;
        result = NumberA - NumberB;
        <span style="color: #0000ff">return</span> result;
    }
}</pre>
<p>OperationMul.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> OperationMul : Operation
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">double</span> GetResult()
    {
        <span style="color: #0000ff">double</span> result = 0;
        result = NumberA * NumberB;
        <span style="color: #0000ff">return</span> result;
    }
}</pre>
<p>OperationDiv.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> OperationDiv : Operation
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">double</span> GetResult()
    {
        <span style="color: #0000ff">double</span> result = 0;
        <span style="color: #0000ff">if</span> (NumberB == 0)
            <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception(&quot;<span style="color: #8b0000">除数不能为0。</span>&quot;);
        result = NumberA / NumberB;
        <span style="color: #0000ff">return</span> result;
    }
}</pre>
<p>OperationFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> OperationFactory
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Operation CreateOperate(<span style="color: #0000ff">string</span> operate)
    {
        Operation oper = <span style="color: #0000ff">null</span>;
        <span style="color: #0000ff">switch</span> (operate)
        {
            <span style="color: #0000ff">case</span> &quot;<span style="color: #8b0000">+</span>&quot;:
                oper = <span style="color: #0000ff">new</span> OperationAdd();
                <span style="color: #0000ff">break</span>;
            <span style="color: #0000ff">case</span> &quot;<span style="color: #8b0000">-</span>&quot;:
                oper = <span style="color: #0000ff">new</span> OperationSub();
                <span style="color: #0000ff">break</span>;
            <span style="color: #0000ff">case</span> &quot;<span style="color: #8b0000">*</span>&quot;:
                oper = <span style="color: #0000ff">new</span> OperationMul();
                <span style="color: #0000ff">break</span>;
            <span style="color: #0000ff">case</span> &quot;<span style="color: #8b0000">/</span>&quot;:
                oper = <span style="color: #0000ff">new</span> OperationDiv();
                <span style="color: #0000ff">break</span>;
        }
        <span style="color: #0000ff">return</span> oper;
    }
}</pre>
<p>Client.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> Client
{
    <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)
    {
        Operation oper;
        oper = OperationFactory.CreateOperate(&quot;<span style="color: #8b0000">+</span>&quot;);
        oper.NumberA = 1;
        oper.NumberB = 2;
        <span style="color: #0000ff">double</span> result = oper.GetResult();
        Console.WriteLine(result);
    }
}</pre>
<p><strong>小结：</strong></p>
<ul>
<li><strong>模式分析：简单工厂模式根据提供给它的数据，返回几个类中的一个类的实例。通常返回的类都有一个公共父类和公共方法。</strong> </li>
<li><strong>优点：工厂类含有必要的判断逻辑，可以决定在什么时候创建哪一个产品类的实例，客户端可以免除直接创建产品对象的责任，通过这种方法实现了对责任的分割。</strong> </li>
<li><strong>缺点：工厂类集中了所有产品创建逻辑，一旦不能工作，整个系统都要受到影响。同时，系统扩展困难，一旦添加新产品就不得不修改工厂逻辑。</strong> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.acmind.com/archives/94/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

