需求分析:现在有一个烧水器假设水温升高到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. MYSQL免安装版使用说明

    1>把压缩文件mysql-noinstall-5.1.6-alpha-win32.zip解压到一个目录下,在环境变量中设置MYSQL_HOME,把%MYSQL_HOME%\bin 加入到 pat ...

  2. CSS兼容性常见问题总结

    DIV+CSS设计IE6.IE7.FF 兼容性 DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使 ...

  3. Sql存储过程解密方法

    在网上查到这样一个存储过程解密的方法,用起来简单,收藏到这里: )) AS ------------------------sql2000大于40000的----------------- --原作: ...

  4. Repeater里面加上if判断

    //创建时绑定 protected void ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.DataItem != ...

  5. (原)opencv中使用限制对比度自适应直方图均衡CLAHE

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462656.html Ptr<CLAHE> clahe = createCLAHE(); ...

  6. [Leetcode] Longest Substring Without Repeating Characters (C++)

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  7. oracle 联表更新

    依 a 表 cate_pub_id  为依据 更新 v 表的 cate_pub_id update td_tobrel_cate_pub_attrval v set v.CATE_PUB_ID=(se ...

  8. 1.Perl基础系列之WHAT、WHY、HOW

    What? Perl,一种功能丰富的计算机程序语言,运行在超过100种计算机平台上,适用广泛,从大型机到便携设备,从快速原型创建到大规模可扩展开发. Why? Perl追求简洁快速地解决问题,可很方便 ...

  9. Measuring Lengths in Baden

    Description Measuring Lengths in Baden time limit per test: 2 seconds memory limit per test: 256 meg ...

  10. iOS GCD使用整理

    自己进行一个复习整理 1.最简单的用法 全局并行 dispatch_async(dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_ ...