大话设计模式笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daska110/article/details/80322760

来自大话设计模式

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

        //}
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页