当前位置: 首页 > news >正文

用C#改写Head First Design Patterns--Command 命令(原创)

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

 

 

 

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

 

相关文章:

  • 海康威视复赛题 ---- 碰撞避免方案(1)
  • C# 中的常用正则表达式总结
  • Node 即学即用 笔记 思维导图
  • 程序员的十层楼:大家都来测测你的技术层级
  • Snail—Hibernate之自写固定组件
  • 购机不求人 液晶电视性能参数全面***
  • Sql Server 中如果使用TransactionScope开启一个分布式事务,使用该事务两个并发的连接会互相死锁吗...
  • Java三大变量小结
  • WPF/Silverlight深蓝团队悄悄登陆了
  • 限制Terminal Server用户登录时间
  • 大话C与Lua(五) 面向对象的数据结构——userdata
  • JAX-WS客户端中引用jar包中的WSDL文件的方法
  • 微软修复了Office套件中“保护视图”功能可被绕过的一个安全漏洞
  • 静电导致笔记本不能开机?
  • .pyc 想到的一些问题
  • [译]Python中的类属性与实例属性的区别
  • Akka系列(七):Actor持久化之Akka persistence
  • Codepen 每日精选(2018-3-25)
  • PHP的类修饰符与访问修饰符
  • ViewService——一种保证客户端与服务端同步的方法
  • vue.js框架原理浅析
  • Webpack 4 学习01(基础配置)
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 从零开始学习部署
  • 大快搜索数据爬虫技术实例安装教学篇
  • 技术发展面试
  • 简单易用的leetcode开发测试工具(npm)
  • 理清楚Vue的结构
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 那些被忽略的 JavaScript 数组方法细节
  • 前端攻城师
  • 我建了一个叫Hello World的项目
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 大数据全解:定义、价值及挑战
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #### go map 底层结构 ####
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #HarmonyOS:Web组件的使用
  • (27)4.8 习题课
  • (3)llvm ir转换过程
  • (4.10~4.16)
  • (52)只出现一次的数字III
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (层次遍历)104. 二叉树的最大深度
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (循环依赖问题)学习spring的第九天
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • .Net - 类的介绍
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .net 反编译_.net反编译的相关问题
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明