设计模式学习篇(一)

直接上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using ConsoleApp.svcTest;
using System.Xml;



namespace ConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //简单工厂模式
            if (false)
            {
                Operate op = SimpleFactory.CreateOperate("+");
                op.NumberA = 20;
                op.NumberB = 30;
                var result = op.GetResult();
                Console.WriteLine(result);
            }
            //策略模式
            if (false)
            {
                StrategyPattern sp = new StrategyPattern("-");
                var result = sp.GetResult(30, 20);
                Console.WriteLine(result);
            }
            //装饰模式
            if (false)
            {
                OuStyle os = new OuStyle();
                os.AddStyle(new PingFang());
                os.Show();  //  欧式风格  这是平房


                ChineseStyle cs = new ChineseStyle();
                cs.AddStyle(os);  //用欧式风格装饰中国风
                cs.Show();// 中国分隔 欧式风格  这是平房

            }
            //代理模式
            if (false)
            {
                IGiveGifts sender = new Persuit();
                Agent agent = new Agent(sender);
                agent.GiveDolls();

            }
            //工厂方法模式
            if (false)
            {
                IFactory factory = new AddOperateFactory();
                Operate op = factory.CreateOperate();
                op.NumberA = 20;
                op.NumberB = 30;
                Console.WriteLine(op.GetResult());
            }
            //状态模式
            if (false)
            {
                Worke w = new Worke {Hour = 10};
                w.WritePrograme();
                w.Hour = 13;
                w.WritePrograme();
                w.Hour = 16;
                w.WritePrograme();
                w.Hour = 9;
                w.WritePrograme();
            }
            //适配器模式
            if (true)
            {
                Player p1 = new Forwards() {Name = "科比"};
                Player p2=new Translater(){Name = "姚明"};
                p1.Attack();
                p1.Defense();
                p2.Attack();
                p2.Defense();
            }

            Console.ReadKey();
        }
    }

    #region 1、简单工厂模式

    public abstract class Operate
    {
        public int NumberA { get; set; }
        public int NumberB { get; set; }

        public virtual int GetResult()
        {
            return 0;
        }
    }

    internal class AddOperate : Operate
    {
        public override int GetResult()
        {
            return this.NumberA + this.NumberB;
        }
    }

    internal class SubOperate : Operate
    {
        public override int GetResult()
        {
            return this.NumberA - this.NumberB;
        }
    }

    /// <summary>
    /// 1、简单工厂模式
    /// </summary>
    internal class SimpleFactory
    {
        public static Operate CreateOperate(string flag)
        {
            switch (flag)
            {
                case "+":
                    return new AddOperate();
                    break;
                case "-":
                    return new SubOperate();
                    break;
                default:
                    return null;
            }
        }
    }

    #endregion

    #region 2、策略模式

    /// <summary>
    /// 在简单工厂模式的基础上,在工厂(类)中,对对象方法进行了再封装 而不是返回对象
    /// </summary>
    internal class StrategyPattern
    {
        private Operate op = null;

        public StrategyPattern(string flag)
        {
            switch (flag)
            {
                case "+":
                    op = new AddOperate();
                    break;

                case "-":
                    op = new SubOperate();
                    break;
                default:
                    op = null;
                    break;
            }
        }

        public int GetResult(int a, int b)
        {
            op.NumberA = a;
            op.NumberB = b;
            return op.GetResult();
        }
    }

    #endregion

    #region 3、装饰模式

    /// <summary>
    /// 抽象接口
    /// </summary>
    internal abstract class House
    {
        public abstract void Show();
    }

    internal class PingFang : House
    {
        public override void Show()
        {
            Console.WriteLine("这是平房");
        }
    }

    internal class LouFang : House
    {
        public override void Show()
        {
            Console.WriteLine("这是楼房");
        }
    }

    /// <summary>
    /// 装饰模式:在当前类中定义继承的类成员,再加个初始该成员的方法,用实现继承类的其它类对象来武装装饰自己
    /// </summary>
    internal class OuStyle : House
    {
        /// <summary>
        /// 将定义用来装饰的对象方法的接口 定义在当前(需要装饰的)类中
        /// </summary>
        private House home = null;

        public override void Show()
        {
            if (null != this.home)
            {
                home.Show();
            }
            Console.WriteLine("欧式风格");

        }

        /// <summary>
        /// 动态添加特定的实现了接口House的类,以便给本类来调用其方法来装饰(武装)自己,
        /// </summary>
        /// <param name="h"></param>
        public void AddStyle(House h)
        {
            this.home = h;
        }

    }

    internal class ChineseStyle : House
    {
        private House h = null;

        public override void Show()
        {
            if (null != h)
            {
                h.Show();
            }
            Console.WriteLine("这是中国风");

        }

        public void AddStyle(House house)
        {
            this.h = house;
        }

    }


    #endregion

    #region 4、代理模式

    public interface IGiveGifts
    {
        void GiveDolls();
        void GiveFlowers();
    }

    public class Persuit : IGiveGifts
    {

        public void GiveDolls()
        {
            Console.WriteLine("送芭比娃娃");
        }

        public void GiveFlowers()
        {
            Console.WriteLine("送花");
        }
    }

    /// <summary>
    /// 什么是代理模式:本来有一个类A可以直接执行自己的方法就可以实现一个功能,现在先将这个类A作为一个属性传递给一个代理类,代理类在通过自己的方法调用A对象的方法,同时可以添加一些新的功能
    /// </summary>
    public class Agent : IGiveGifts
    {
        private IGiveGifts sender;

        public Agent(IGiveGifts ig)
        {
            this.sender = ig;
        }

        public void GiveDolls()
        {
            sender.GiveDolls();
            Console.WriteLine("我是**代理 送芭比娃娃");
        }

        public void GiveFlowers()
        {
            sender.GiveFlowers();
            Console.WriteLine("我是**代理 送花");

        }


    }

    #endregion

    #region 5、工厂方法模式

    /// <summary>
    /// 工厂模式存在类与switch语句的高耦合,增加新的类 需要去增加case分支,违背了开放-封闭原则 工厂方法模式可以解决这个问题。
    /// </summary>
    public interface IFactory
    {
        Operate CreateOperate();
    }

    public class AddOperateFactory : IFactory
    {
        public Operate CreateOperate()
        {
            return new AddOperate();
        }
    }

    public class SubOperateFactory : IFactory
    {

        public Operate CreateOperate()
        {
            return new SubOperate();
        }
    }

    #endregion

    #region 6、状态模式

    //状态抽象类
    public abstract class State
    {
        public abstract void WritePrograme(Worke w);
    }

    /// <summary>
    /// 子状态A 
    /// </summary>
    public class StateA : State
    {
        public override void WritePrograme(Worke w)
        {
            if (w.Hour<12)
            {
                Console.WriteLine("当前{0}点,工作状态良",w.Hour);
            }
            else
            {
                w.CurrentState=new StateB();
                w.WritePrograme();
                
            }
        }
    }


    /// <summary>
    /// 子状态B
    /// </summary>
    public class StateB : State
    {
        public override void WritePrograme(Worke w)
        {
            if (w.Hour<14)
            {
                Console.WriteLine("当前时间{0},午休中,切勿打扰");
            }
            else
            {
                w.CurrentState = new StateC();
                w.WritePrograme();
            }

        }
    }


    /// <summary>
    /// 当前状态C
    /// </summary>
    public class StateC:State
    {
        public override void WritePrograme(Worke w)
        {
            if (w.Hour<18)
            {
                Console.WriteLine("当前时间{0},下午工作状态略低",w.Hour);
            }
        }
    }


    //工作类
    public class Worke
    {
        /// <summary>
        /// 当前状态
        /// </summary>
        public State CurrentState { get; set; }

        public Worke()
        {
            this.CurrentState=new  StateA();
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        public Worke(State s,int hour)
        {
            this.CurrentState = s;
            this.Hour = hour;
        }

        /// <summary>
        /// 当前时间
        /// </summary>
        public int Hour { get; set; }

        public void WritePrograme()
        {
            CurrentState.WritePrograme(this);
        }
    }

    #endregion

    #region 适配器模式

    public abstract class Player
    {
        public string Name { get; set; }

        public abstract void Attack();

        public abstract void Defense();

    }

    public class Forwards : Player
    {
        public override void Attack()
        {
            Console.WriteLine("中锋进攻");
        }

        public override void Defense()
        {
            Console.WriteLine("中锋防守");
        }
    }

    public class ForeignPlayer 
    {
        public string Name { get; set; }

        public void 进攻()
        {
            Console.WriteLine("外籍球员进攻");
        }

        public void 防守()
        {
            Console.WriteLine("外籍球员防守");
        }
    }

    public class Translater : Player
    {
        private ForeignPlayer play=new ForeignPlayer();

        public override void Attack()
        {
            play.进攻();
        }

        public override void Defense()
        {
            play.防守();
        }
    }



    #endregion
    
    internal class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
View Code
原文地址:https://www.cnblogs.com/lxf1117/p/4943375.html