<?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/%e5%b7%a5%e5%8e%82/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>抽象工厂模式</title>
		<link>http://blog.acmind.com/archives/129</link>
		<comments>http://blog.acmind.com/archives/129#comments</comments>
		<pubDate>Sat, 17 Jan 2009 02:10:26 +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=129</guid>
		<description><![CDATA[ 
User.cs
class User
{
    private int _id;
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    private string _name;
  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.acmind.com/wp-content/uploads/2009/01/image11.png" class="highslide-image" onclick="return hs.expand(this);"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="500" alt="image" src="http://blog.acmind.com/wp-content/uploads/2009/01/image-thumb11.png" width="652" border="0" /></a> </p>
<p>User.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> User
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _id;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ID
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _id; }
        <span style="color: #0000ff">set</span> { _id = <span style="color: #0000ff">value</span>; }
    }

    <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _name;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> NAME
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _name; }
        <span style="color: #0000ff">set</span> { _name = <span style="color: #0000ff">value</span>; }
    }
}</pre>
<p>IUser.cs</p>
<pre class="mycode"><span style="color: #0000ff">interface</span> IUser
{
    <span style="color: #0000ff">void</span> Insert(User user);

    User GetUser(<span style="color: #0000ff">int</span> id);
}</pre>
<p>SqlserverUser.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> SqlserverUser : IUser
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Insert(User user)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在SQL Server中给User表增加一条记录。</span>&quot;);
    }

    <span style="color: #0000ff">public</span> User GetUser(<span style="color: #0000ff">int</span> id)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在SQL Server中根据ID得到User表一条记录。</span>&quot;);
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;
    }
}</pre>
<p>AccessUser.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> AccessUser : IUser
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Insert(User user)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在Access中给User表增加一条记录。</span>&quot;);
    }

    <span style="color: #0000ff">public</span> User GetUser(<span style="color: #0000ff">int</span> id)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在Access中根据ID得到User表一条记录。</span>&quot;);
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;
    }
}</pre>
<p>Department.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> Department
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _id;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ID
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _id; }
        <span style="color: #0000ff">set</span> { _id = <span style="color: #0000ff">value</span>; }
    }

    <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _deptName;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> DeptName
    {
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _deptName; }
        <span style="color: #0000ff">set</span> { _deptName = <span style="color: #0000ff">value</span>; }
    }
}</pre>
<p>IDepartment.cs</p>
<pre class="mycode"><span style="color: #0000ff">interface</span> IDepartment
{
    <span style="color: #0000ff">void</span> Insert(Department department);

    Department GetDepartment(<span style="color: #0000ff">int</span> id);
}</pre>
<p>SqlserverDepartment.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> SqlserverDepartment : IDepartment
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Insert(Department department)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在SQL Server中给Department表增加一条记录。</span>&quot;);
    }

    <span style="color: #0000ff">public</span> Department GetDepartment(<span style="color: #0000ff">int</span> id)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在SQL Server中根据ID得到Department表一条记录。</span>&quot;);
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;
    }
}</pre>
<p>AccessDepartment.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> AccessDepartment : IDepartment
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Insert(Department department)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在Access中给Department表增加一条记录。</span>&quot;);
    }

    <span style="color: #0000ff">public</span> Department GetDepartment(<span style="color: #0000ff">int</span> id)
    {
        Console.WriteLine(&quot;<span style="color: #8b0000">在Access中根据ID得到Department表一条记录。</span>&quot;);
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;
    }
}</pre>
<p>IFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">interface</span> IFactory
{
    IUser CreateUser();

    IDepartment CreateDepartment();
}</pre>
<p>SqlserverFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> SqlserverFactory : IFactory
{
    <span style="color: #0000ff">public</span> IUser CreateUser()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> SqlserverUser();
    }

    <span style="color: #0000ff">public</span> IDepartment CreateDepartment()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> SqlserverDepartment();
    }
}</pre>
<p>AccessFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> AccessFactory : IFactory
{
    <span style="color: #0000ff">public</span> IUser CreateUser()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AccessUser();
    }

    <span style="color: #0000ff">public</span> IDepartment CreateDepartment()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AccessDepartment();
    }
}</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)
    {
        User user = <span style="color: #0000ff">new</span> User();
        Department department = <span style="color: #0000ff">new</span> Department();

        IFactory factory = <span style="color: #0000ff">new</span> SqlserverFactory();
        <span style="color: #008000">//IFactory factory = new AccessFactory();</span>

        IUser iu = factory.CreateUser();
        iu.Insert(user);
        iu.GetUser(1);

        IDepartment id = factory.CreateDepartment();
        id.Insert(department);
        id.GetDepartment(1);

        Console.Read();
    }
}</pre>
<p><strong>小结：</strong></p>
<ul>
<li><strong>本例分析：只有一个User类和User操作类时，是只需要工厂模式的，但现在加入新表，而且SQL Server与Access又是两大不同的分类，所以解决这种涉及多个产品系列的问题，用到了抽象工厂模式。</strong> </li>
<li><strong>优点：最大的好处是易于交换产品系列，由于具体工厂类，如IFactory factory=new AccessFactory()，在一个应用中只需要在初始化时出现一次，这就使得改变一个应用的具体工厂变得非常容易，它只需要改变具体工厂即可使用不同的产品配置；第二大好处是，它让具体的创建实例过程与客户端分离，客户端是通过它们的抽象接口操纵实例，产品的具体类名也被具体工厂的实现分离，不会出现在客户代码中。</strong> </li>
<li><strong>缺点：比如我们要增加项目表Project，至少要增加三个类，IProject、SqlserverProject、AccessProject，还需要更改IFactory、SqlserverFactory和AccessFactory才可以完全实现；另外，客户端程序类不只一个时，很多地方都在用IUser或IDepartment，每一个类在开始都会声明IFactory factory=new SqlserverFactory()，如果我有100个调用数据库的类，就要更改100次IFactory factory=new SqlserverFactory()。［此种情况时可以考虑用简单工厂模式＋抽象工厂模式进行，随后也可以再过渡到反射＋抽象工厂模式（反射＋配置文件）］。</strong> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.acmind.com/archives/129/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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/121</link>
		<comments>http://blog.acmind.com/archives/121#comments</comments>
		<pubDate>Sat, 17 Jan 2009 01:54:43 +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=121</guid>
		<description><![CDATA[ 
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/image10.png" class="highslide-image" onclick="return hs.expand(this);"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="393" alt="image" src="http://blog.acmind.com/wp-content/uploads/2009/01/image-thumb10.png" width="677" border="0" /></a> </p>
<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>IFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">interface</span> IFactory
{
    Operation CreateOperation();
}</pre>
<p>AddFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> AddFactory : IFactory
{
    <span style="color: #0000ff">public</span> Operation CreateOperation()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationAdd();
    }
}</pre>
<p>SubFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> SubFactory : IFactory
{
    <span style="color: #0000ff">public</span> Operation CreateOperation()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationSub();
    }
}</pre>
<p>MulFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> MulFactory : IFactory
{
    <span style="color: #0000ff">public</span> Operation CreateOperation()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationMul();
    }
}</pre>
<p>DivFactory.cs</p>
<pre class="mycode"><span style="color: #0000ff">class</span> DivFactory : IFactory
{
    <span style="color: #0000ff">public</span> Operation CreateOperation()
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationDiv();
    }
}</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)
    {
        IFactory operFactory = <span style="color: #0000ff">new</span> AddFactory();
        Operation oper = operFactory.CreateOperation();
        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>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.acmind.com/archives/121/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>

