大话设计模式-工厂方法模式(8)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    interface IFactory
    {
        Operation CreateOperation();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    class AddFactory:IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationAdd();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    class SubFactory:IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationSub();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    class DivFactory:IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationDiv();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    class MulFactory:IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationMul();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    /// <summary>
    /// 抽象父类
    /// </summary>
    public class Operation
    {
        private double numberA = 0;
        private double numberB = 0;

        public double NumberA
        {
            get { return numberA; }
            set { numberA = value; }
        }

        public double NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    public class OperationDiv:Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
            {
                throw new Exception("除数不能为0");
            }
            result = NumberA / NumberB;
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    public class OperationMul:Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    public class OperationSub:Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodsFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入数字A:");
            string numberA = Console.ReadLine();

            Console.Write("请输入数字B:");
            string numberB = Console.ReadLine();

            IFactory operFactory = new AddFactory();
            Operation oper = operFactory.CreateOperation();
            oper.NumberA = Convert.ToDouble(numberA);
            oper.NumberB = Convert.ToDouble(numberB);

            double result = oper.GetResult();

            Console.Write("计算结果是:" + result.ToString());

            Console.ReadLine();
        }
    }
}

 

工厂方法模式

  工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个工厂。工厂方法使一个类的实例化延迟到其子类.

 

简单工厂与工厂方法

  1.简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。

  2.工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,

     工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,而现在是修改客户端.

posted @ 2016-03-14 15:48  如.若  阅读(223)  评论(0编辑  收藏  举报