用C#改写Head First Design PatternsCommand 命令(原创)

用一个控制器(带有几个开关)来控制所有的电器?神奇而简单的实现:

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

namespace Command
{
    public interface Command
    {
        void execute();
        void execute1();

        void execute2();
    }
}

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

namespace Command
{
    /// <summary>
    /// 电灯类
    /// </summary>
    public class Light
    {
        public Light(){}

        public void on()
        {
            System.Console.WriteLine("开灯");
        }

        public void off()
        {
            System.Console.WriteLine("关灯");
        }
    }

    /// <summary>
    /// 电视类,具有不同的方法
    /// </summary>
    public class TV
    {
        public TV() { }

        public void open()
        {
            System.Console.WriteLine("开电视");
        }

        public void close()
        {
            System.Console.WriteLine("关电视");
        }

        public void changeChannel()
        {
            System.Console.WriteLine("换台");
        }

        public void setVolume(int i)
        {
            System.Console.WriteLine("控制音量"+i.ToString());
        }

    }
}

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

namespace Command
{
    public class LightCommands : Command
    {
        Light light;

        public LightCommands(Light l)
        {
            this.light = l;
        }


        #region Command 成员

        public void execute()
        {
            light.on();
        }


        void Command.execute1()
        {
            light.off();
        }

        //啥也不做!
        void Command.execute2()
        {
            //throw new NotImplementedException();

        }

        #endregion
    }

    public class TVCommands : Command
    {
        TV tv;

        public TVCommands(TV tv)
        {
            this.tv = tv;
        }


        #region Command 成员

        public void execute()
        {
            tv.open();
        }

        void Command.execute1()
        {
            tv.close();
        }

        /// <summary>
        /// 特别的操作:换台
        /// </summary>
        void Command.execute2()
        {
            tv.changeChannel();
        }

        #endregion
    }
}

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

namespace Command
{

    /// <summary>
    /// 遥控器,要把它作为万能的控制中心
    /// </summary>
    public class ControlCenter
    {
        Command com;

        public ControlCenter() { }

        public void setCommand(Command c)
        {
            this.com = c;
        }

        public void firstButtonPressed()
        {
            com.execute();
        }

        public void secondButtonPressed()
        {
            com.execute1();
        }

        /// <summary>
        /// 特殊的按钮,用于处理特别需求
        /// </summary>
        public void SpecButtonPressed()
        {
            com.execute2();
        }

    }
}

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

namespace Command
{
    class Program
    {
        static void Main(string[] args)
        {
            ControlCenter cc = new ControlCenter();
           
            Light l=new Light();
            Command c = new LightCommands(l);

            TV t = new TV();
            Command c1 = new TVCommands(t);

            cc.setCommand(c);
            cc.firstButtonPressed();
            cc.SpecButtonPressed();
            cc.secondButtonPressed();
            cc.setCommand(c1);
            cc.firstButtonPressed();
            cc.SpecButtonPressed();
            cc.secondButtonPressed();


            System.Console.ReadLine();
        }
    }
}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1519205.html