標籤:style blog http color 使用 os io strong
需求分析:現在有一個燒水器假設水溫升高到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 = 0; /// <summary> /// 開關 /// </summary> class Switcher { public void SwitchOn() { Console.WriteLine("熱水器被開啟"); } public void SwitchOff() { Console.WriteLine("熱水器被關閉"); } } /// <summary> /// 熱水器 /// </summary> class Heater { public void BoidWater() { int i = 0; for (i = 0; i <= 100; i++) { tempture = i; Thread.Sleep(1000 / (i + 1)); if (tempture < 95 && tempture % 10 == 0) { Console.WriteLine("燒水中..."); } else { MakeAlerm(tempture); ShowTempture(tempture); } } } public static void MakeAlerm(int tem) { if (tem > 95) { Console.WriteLine("通報器:“嘀嘀嘀,水快燒開了.....”"); } } private static void ShowTempture(int tempture) { if (tempture > 95) { if (tempture == 100) { 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(); } }}View Code
不信你就看:應該還滿足需求吧.
這樣寫我根本就沒動腦子,說實話連垃圾都不如,沒一點價值,完全以一個碼農去完成了。現在我們換個思維去思考,設計模式中是不是有一種叫做觀察者模式的,我們可不可以藉助觀察者模式去試下呢?既然是觀察者模式肯定有像觀察者一樣的對象,我感覺此處的觀察者就是熱水器,它即時監控著水溫,而水溫是其他事件的觸發者,下面我們看看這次寫的代碼:
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(); } }}
此次運行結果:
同樣的功能,不一樣的代碼,後者是不是有種高大上的感覺。