需求分析:现在有一个烧水器假设水温升高到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# 热水器的更多相关文章

  1. C#学习笔记(11)——深入事件,热水器案例

    说明(2017-6-14 15:04:13): 1. 热水器案例,为了便于理解,采用了蹩脚但直观的英文命名,哼哼. heater类,加热,声明一个委托,定义一个委托事件: using System; ...

  2. 重温Observer模式--热水器·改

    引言 在 C#中的委托和事件 一文的后半部分,讲述了Observer(观察者)模式,并使用委托和事件实现了这个模式.实际上,不使用委托和事件,一样可以实现Observer模式.在本文中,我将使用GOF ...

  3. .NET面试题系列[7] - 委托与事件

    委托和事件 委托在C#中具有无比重要的地位. C#中的委托可以说俯拾即是,从LINQ中的lambda表达式到(包括但不限于)winform,wpf中的各种事件都有着委托的身影.C#中如果没有了事件,那 ...

  4. Linux下UPnP sample分析

        一.UPnP简介   UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...

  5. 安卓APP与智能硬件相结合的简易方案

    第1章 概  述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源   http://www. ...

  6. C# 中的委托和事件

    觉得这篇文章写的非常好,大神之作,由简入繁,对我这种初学者来说帮忙很大,特此留存下. 摘自:http://tracefact.net/CSharp-Programming/Delegates-and- ...

  7. 《.NET之美》消息及勘误

    <.NET之美>消息及勘误 编辑最终还是采用了<.NET之美>作为书名,尽管我一直觉得这个名字有点文艺了,而更倾向于使用<.NET专题解析>这个名称. 目前已经可以 ...

  8. Alljoyn瘦客户端库介绍(官方文档翻译 下)

    由于其他事情耽误,这个翻译现在才完成.接上篇—— 4 瘦客户端核心库架构 由于AllJoyn瘦客户端核心库(AJTCL)必须运行在那些功耗受限.计算能力有限.资源紧缺的设备上,因此它无法像运行在通用型 ...

  9. C# ~ 从 委托事件 到 观察者模式 - Observer

    委托和事件的部分基础知识可参见 C#/.NET 基础学习 之 [委托-事件] 部分: 参考 [1]. 初识事件 到 自定义事件: [2]. 从类型不安全的委托 到 类型安全的事件: [3]. 函数指针 ...

随机推荐

  1. c#中的表达式

    // 把变量和字面值(在使用运算符时,将它们统称为操作数)与运算符组合起来 // 就可以创建表达式,表达式是计算的基本构件 // 操作数可以是数值也可以是变量 + ; ; int num3 = num ...

  2. Javascript进阶篇——( JavaScript内置对象---下)--Math对象---笔记整理

    Math对象使用 Math 的属性和方法: <script type="text/javascript"> var mypi=Math.PI; var myabs=Ma ...

  3. 最近因为textview高度问题疯了疯了疯了

    1.textview有\r\n什么的就算不明白,我的文本最后一个字符是\r,结果我死活算不对,最后发现了==! NSString * str = [_messageModels[indexPath.r ...

  4. (转) eclipse debug (调试) 学习心得

    1.Step Into (also F5) 跳入2.Step Over (also F6) 跳过3.Step Return (also F7) 执行完当前method,然后return跳出此metho ...

  5. HDU-简单计算器-1237

    这道题我做了一天,把中缀表达式转化为后缀表达式,但遇到了储存的问题,考虑了好久,写出后又调试,弄了一天,下面说一下中缀表达式转换后缀表达式: 算法: 中缀表达式转后缀表达式的方法: 1.遇到操作数:直 ...

  6. jetty 8.1.8 PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required

    应该是jdk和jre配置问题,建议看看这个博客:http://blog.csdn.net/nba_2011/article/details/7219750里边查看配置方法很清楚

  7. centos 6.4从源码安装mysql 5.6笔记

    上周在安装mysql时遇到了些许麻烦,今天整理下. 在代码目录建立obj文件夹. 在obj目录下,执行cmake .. -DXXX  // XXX表示一些参数,详见http://dev.mysql.c ...

  8. 【Lucene4.8教程之五】Luke

    一.Luke基本内容 1.Luke简介 Luke可用于查看Lucene创建的索引,并对其进行基本操作. 2.创建Luke (1)从Github上下载源文件 https://github.com/tar ...

  9. Python之路第十一天,高级(3)-线程池

    线程池 简单的线程池的实现: import queue import threading import time class ThreadPool(object): def __init__(self ...

  10. python运维开发(二十)----models操作、中间件、缓存、信号、分页

    内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标 ...