需求分析:现在有一个烧水器假设水温升高到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();
}
}
}
此次运行结果:
同样的功能,不一样的代码,后者是不是有种高大上的感觉。