简单工厂模式学习

   在设计模式中,用到最多的就是工厂模式了,工厂模式又分为:简单工厂模式、抽象工厂模式、工厂方法模式,而在工厂中,我用到最多就是简单工厂模式,简单工厂模式的优点在我看来就是:

   扩展性比较好,因为各具体实现类都继承自父类,如果再增加一个类似操作方法类,那就让此类再次继承自父类,在工厂类中添加一个判断就行了,而不用修改原来的实现类。

   其它的还未体会到,先贴一段具体代表性的代码吧,从《大话设计模式》中摘抄。

class Program

    {

        static void Main(string[] args)

        {

            Operation m_Operation = OperationFactory.createOperation("+");

            m_Operation.NumberA = 10;

            m_Operation.NumberB = 8;

            Console.Write("结果是:{0}",m_Operation.GetResult().ToString());

            Console.Read();

        }

    }

    /// <summary>

    /// 操作基类

    /// </summary>

    public class Operation

    {

        private double m_NumberA = 0;



        public double NumberA

        {

            get { return m_NumberA; }

            set { m_NumberA = value; }

        }

        private double m_NumberB = 0;



        public double NumberB

        {

            get { return m_NumberB; }

            set { m_NumberB = value; }

        }

        public virtual double GetResult()

        {

            double result = 0;

            return result;

        }

    }

    /// <summary>

    /// 加法类

    /// </summary>

    class OperationAdd : Operation

    {

        public override double GetResult()

        {

            double result = 0;

            result = this.NumberA + this.NumberB;

            return result;

        }

    }

    /// <summary>

    /// 减法

    /// </summary>

    class OperationSub : Operation

    {

        public override double GetResult()

        {

            double result = 0;

            result = this.NumberA - this.NumberB;

            return result;

        }

    }

    /// <summary>

    /// 乘法

    /// </summary>

    class OperationMul : Operation

    {

        public override double GetResult()

        {

            double result = 0;

            result = this.NumberA * this.NumberB;

            return result;

        }

    }

    /// <summary>

    /// 除法

    /// </summary>

    class OperationDiv : Operation

    {

        public override double GetResult()

        {

            double result = 0;

            if (NumberB == 0)

            {

                throw new Exception("除数不能为0!");

            }

            result = this.NumberA / this.NumberB;

            return result;

        }

    }

    public class OperationFactory

    {

        public static Operation createOperation(string p_Operation)

        {

            Operation m_Operation = null;

            switch (p_Operation)

            {

                case "+":

                    m_Operation = new OperationAdd();

                    break;

                case "-":

                    m_Operation = new OperationSub();

                    break;

                case "*":

                    m_Operation = new OperationMul();

                    break;

                case "/":

                    m_Operation = new OperationDiv();

                    break;

            }

            return m_Operation;

        }

    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值