zoukankan      html  css  js  c++  java
  • 用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
  • 相关阅读:
    [例程]string.trim().length()的用法
    用各种look and feel打造swing界面
    深入浅出Java多线程(1)方法 join
    eclipse中cvs使用配置
    什么时候用Vector, 什么时候改用ArrayList?
    array,vertor,arraylist,hashable,hashmap等几个易混淆概念的区别
    java.lang.Class.getResource()这哥个方法主要是做什么用
    织梦dedecms实现按照字母搜索的实现方法
    浅析JTable与TableModel、TableCellRenderer、TableCellEditor接口——使用JComboBox显示单元格的值
    用java –jar 命令运行Jar包
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519205.html
Copyright © 2011-2022 走看看