博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用C#改写Head First Design Patterns--Command 命令(原创)
阅读量:4134 次
发布时间:2019-05-25

本文共 2537 字,大约阅读时间需要 8 分钟。

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

 

 

 

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

 

转载地址:http://gppvi.baihongyu.com/

你可能感兴趣的文章
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
异常 Java学习Day_15
查看>>
Mysql初始化的命令
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
JAVA数据类型
查看>>
【Python】学习笔记——-6.2、使用第三方模块
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>