C# 热水器
需求分析:现在有一个烧水器假设水温升高到100度为开水请用设计程序模拟整个烧水的过程,打开热水器,当水温达到95度时报警器开始发出警报,水温达到100度时,断开热水器电源。
我们使用常规的思维去分析这个需求,不就一个开关控制,一个警报吗?不假思索代码就好了,我们来看看下面的垃圾代码啊。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace heater
{
class Program
{
static int tempture = ;
/// <summary>
/// 开关
/// </summary>
class Switcher
{
public void SwitchOn()
{
Console.WriteLine("热水器被打开");
} public void SwitchOff()
{
Console.WriteLine("热水器被关闭");
}
} /// <summary>
/// 热水器
/// </summary>
class Heater
{
public void BoidWater()
{
int i = ;
for (i = ; i <= ; i++)
{
tempture = i;
Thread.Sleep( / (i + ));
if (tempture < && tempture % == )
{
Console.WriteLine("烧水中...");
}
else
{
MakeAlerm(tempture);
ShowTempture(tempture);
}
}
}
public static void MakeAlerm(int tem)
{
if (tem > )
{
Console.WriteLine("报警器:“嘀嘀嘀,水快烧开了.....”");
}
}
private static void ShowTempture(int tempture)
{ if (tempture > )
{
if (tempture == )
{
Console.WriteLine("\n水真的开了\n");
}
else
{
Console.WriteLine("水的温度是:{0}\n", tempture);
}
}
}
}
static void Main(string[] args)
{
Heater heater = new Heater();
Switcher switcher = new Switcher(); switcher.SwitchOn();
heater.BoidWater();
switcher.SwitchOff(); Console.ReadLine();
}
}
}
不信你就看截图:应该还满足需求吧.

这样写我根本就没动脑子,说实话连垃圾都不如,没一点价值,完全以一个码农去完成了。现在我们换个思维去思考,设计模式中是不是有一种叫做观察者模式的,我们可不可以借助观察者模式去试下呢?既然是观察者模式肯定有像观察者一样的对象,我感觉此处的观察者就是热水器,它实时监控着水温,而水温是其他事件的触发者,下面我们看看这次写的代码:
observer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace oberver
{
//开关
public class Switch
{
public void SwitchOn(int tempture)
{
if (tempture <= 0)
{
Console.WriteLine("热水器被打开");
}
}
public void Switchoff(int tempture)
{
if (tempture >= 100)
{
Console.WriteLine("热水器被关闭");
}
}
} //热水器
public class Heater
{
private int tempture;
public delegate void BoildHander(int param); //声明委托
public event BoildHander BoilEvent; //声明事件 //烧水
public void BoilWater()
{
if (BoilEvent != null)
{
BoilEvent(tempture);
}
for (int i = 0; i <= 100; i++)
{
tempture = i;
Thread.Sleep(1000 / (i + 1));
if (i % 10 == 0 && i<100)
{
Console.WriteLine("烧水中...");
}
//Console.WriteLine("烧水中,温度:{0}'",tempture);
if (tempture > 95)
{
if (BoilEvent != null) //如果有注册事件
{
BoilEvent(tempture);//调用所有注册时间的方法
}
}
}
}
}
//警报器
public class Alarm
{
public void MakeAlert(int tempture)
{
if (tempture >= 96)
{
Console.WriteLine("嘀嘀嘀,水已经{0}度了\n", tempture);
}
}
} //显示器
public class Display
{
public static void ShowMsg(int tem)
{
if (tem == 100)
{
Console.WriteLine("水烧开了,当前温度是:{0}度\n", tem);
}
}
} class Progrm
{
static void Main(string[] args)
{
Switch switcher = new Switch();
Heater heater = new Heater();
Alarm alarm = new Alarm();
heater.BoilEvent += switcher.SwitchOn; //注册事件
heater.BoilEvent += alarm.MakeAlert; //注册事件
//heater.BoilEvent += (new Alarm()).MakeAlert; //匿名对象注册方法
heater.BoilEvent += Display.ShowMsg; //注册方法
heater.BoilEvent += switcher.Switchoff; heater.BoilWater(); //开始烧水
Console.Read();
}
}
}
此次运行结果:

同样的功能,不一样的代码,后者是不是有种高大上的感觉。
C# 热水器的更多相关文章
- C#学习笔记(11)——深入事件,热水器案例
说明(2017-6-14 15:04:13): 1. 热水器案例,为了便于理解,采用了蹩脚但直观的英文命名,哼哼. heater类,加热,声明一个委托,定义一个委托事件: using System; ...
- 重温Observer模式--热水器·改
引言 在 C#中的委托和事件 一文的后半部分,讲述了Observer(观察者)模式,并使用委托和事件实现了这个模式.实际上,不使用委托和事件,一样可以实现Observer模式.在本文中,我将使用GOF ...
- .NET面试题系列[7] - 委托与事件
委托和事件 委托在C#中具有无比重要的地位. C#中的委托可以说俯拾即是,从LINQ中的lambda表达式到(包括但不限于)winform,wpf中的各种事件都有着委托的身影.C#中如果没有了事件,那 ...
- Linux下UPnP sample分析
一.UPnP简介 UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...
- 安卓APP与智能硬件相结合的简易方案
第1章 概 述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源 http://www. ...
- C# 中的委托和事件
觉得这篇文章写的非常好,大神之作,由简入繁,对我这种初学者来说帮忙很大,特此留存下. 摘自:http://tracefact.net/CSharp-Programming/Delegates-and- ...
- 《.NET之美》消息及勘误
<.NET之美>消息及勘误 编辑最终还是采用了<.NET之美>作为书名,尽管我一直觉得这个名字有点文艺了,而更倾向于使用<.NET专题解析>这个名称. 目前已经可以 ...
- Alljoyn瘦客户端库介绍(官方文档翻译 下)
由于其他事情耽误,这个翻译现在才完成.接上篇—— 4 瘦客户端核心库架构 由于AllJoyn瘦客户端核心库(AJTCL)必须运行在那些功耗受限.计算能力有限.资源紧缺的设备上,因此它无法像运行在通用型 ...
- C# ~ 从 委托事件 到 观察者模式 - Observer
委托和事件的部分基础知识可参见 C#/.NET 基础学习 之 [委托-事件] 部分: 参考 [1]. 初识事件 到 自定义事件: [2]. 从类型不安全的委托 到 类型安全的事件: [3]. 函数指针 ...
随机推荐
- ECMAScript 6新特性介绍
箭头函数 箭头函数使用=>语法来简化函数.在语句结构上和C#.Java 8 和 CoffeeScript相似,支持表达式和函数体. . =>`操作符左边为输入的參数.而右边则是进行的操作以 ...
- Ubuntu的防火墙UFW
这是个简单的防火墙,可以直接在命令行启停,也可安装提图形端gufw *安装 sudo apt-get install ufw gufw *常用命令 sudo ufw enable //启动 ufw d ...
- iOS之Swift语言的学习
好久都没有来这个熟悉而又陌生的地方啦, 想想已经有两三个月了吧,不过我相信以后还是会经常来的啦,因为忙碌的学习已经过去啦,剩下的就是要好好的总结好好的复习了,好好的熟悉下我们之前学习的知识点,将他们有 ...
- linux 系统下配置安装 java jdk 图文流程
先查看一下系统版本,本例采用的操作系统是CentOS 6.5: 如果你是初装之后的操作系统,那么有可能wget这个组件是不存在的,所以你要安装一下它,这样才可以让你从网上down下你要的安装包: 上面 ...
- ASP.Net请求处理机制初步探索之旅 - Part 1 前奏(转)
在读本文之前建议先阅读IIS架构:http://www.cnblogs.com/tiantianle/p/5079932.html 不管是ASP.Net WebForm还是ASP.Ne ...
- 图片轮播插件 Slides-SlidesJS-3
图片轮播插件 Slides-SlidesJS-3 demo document 地址: http://slidesjs.com/
- Sqlserver 系列(一):常用函数
(1)聚合函数 sum,max,min,avg,count (2)日期函数 datediff ,dateadd, datepart,getdate,month,day (3)字符串函数 ltrim,r ...
- mysql的join
SELECT * FROM a LEFT JOIN b ON a.aID = b.bID; a为主,a的数据全显示,连不上b的对应字段为空 SELECT * FROM a RIGHT JOIN ...
- UGUI学习之InputField
密码框在Context type 设置为Passwold 设置背景和调整字体颜色与透明度. 还有一个就是Toggle,(开关)要指定他的Graphic.
- (原创) mac 10.9.2 eclipse 的 CDT 的 异常的修复
测试平台:macbook air 2012 , os x 10.9.2 , eclipse 4.3 在升级了 10.9 之后,eclipse 的CDT 无法正常使用了 异常表现: 1. 文 ...