大话设计模式笔记

来自大话设计模式

C#入门

猫狗和动物 将面向对象的封装 继承 多态,很形象。
委托和事件是一种通知机制,不属于某个特定的类,而是两个类之间的信息传递。
接口是因为多个类可能都会有相同的方法,比如孙悟空,猪八戒都会变身,但不是所有的动物都会变身,不能给基类加这个方法,因此不用继承而是用接口。这是比C++高端的地方,但是也好复杂。
泛型就是C++里边的STL。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DesignMode2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Cat cat = new Cat("咪咪");
            Cat cat = new Cat("小咪");
            //cat.ShoutNum = 5;
            MessageBox.Show(cat.Shout());
        }
    }

    class Cat
    {
        string name = "";

        // 构造
        public Cat(string name)
        {
            this.name = name;
        }
        public Cat()
        {
            this.name = "mimi";
        }

        // 叫
        public string Shout()
        {
            string result = "";
            for(int i = 0; i < shoutNum; ++i)
            {
                result += "喵";
            }

            //return "我的名字是:" + name + ", 喵";
            return "我的名字是: " + name + " " + result;
        }

        // 变量+存取器
        private int shoutNum = 3;
        public int ShoutNum
        {
            get
            {
                return shoutNum;
            }
            set
            {
                shoutNum = value;
            }
        }
    }
}

工厂模式

C#控制台程序

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

namespace DesignMode
{
    class Program
    {
        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;
            }
        }

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

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

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

        class OperationDev:Operation
        {
            public override double GetResult()
            {
                if (NumberB == 0)
                    throw new Exception("除数不能为0");
                return NumberA / NumberB;
            }
        }

        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 OperationDev();
                        break;
                    default:
                        break;
                }

                return oper;
            }
        }

        static void Main(string[] args)
        {
            Operation oper;
            oper = OperationFactory.createOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();
            Console.WriteLine(oper.NumberA + "+" + oper.NumberB +  "is: " + result);
            Console.Read();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值