设计模式的学习

<span style="color:#FF0000;"><strong>大话设计模式这本书,挺有趣的,讲故事的方式来介绍设计模式,主要是用C#写的,这个博客是我看这本书一边学习一边敲的,确实是写的不错这本书,通熟易懂</strong></span>

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

namespace 设计模式
{
    class Program
    {
        static void Main(string[] args)
        {
            //本程序对计算器程序进行了抽象封装,运用继承和多态,使程序变得可扩展,可维护,安全性高
            //还用了简单工厂模式,根据用户界面的输入来创建相应专门使用的实例

            Operation oper = null;
            oper = OperationFactory.createOperate("+");
            oper.NumberA = 100;
            oper.NumberB = 200;

            Console.WriteLine(oper.GetResult());//打印相加结果300

            Console.ReadKey();//等待输入
        }
    }

    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;
        }
    }

    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }

    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }

    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }

    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA / NumberB;
            return result;
        }
    }


    //工厂模式来控制创建对应的类实例,使得功能扩展方便,使界面和算法完全分离。
    public class OperationFactory
    {
        //静态方法(类名调用)用于创建专门的功能实例
        public static Operation createOperate(string operate)
        {
            Operation oper = null;

            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }
    }
}


以下为类结构图: 




by Wenism

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值