观察者模式--初学入门
生活随笔
收集整理的這篇文章主要介紹了
观察者模式--初学入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
理解不是很深,不做解說,自己寫了個小例子記錄下來
例子以面試者去面試為主題,容易理解,例子簡單(小白寫的,能不簡單嗎),能看的懂的代碼才是好代碼啊
觀察者接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 觀察者接口 11 /// </summary> 12 interface IMSG 13 { 14 /// <summary> 15 /// 出題 16 /// </summary> 17 void ShowMessage(); 18 /// <summary> 19 /// 結果 20 /// </summary> 21 void Result(); 22 } 23 } View Code觀察者
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 面試管 11 /// </summary> 12 public class MSG : IMSG 13 { 14 /// <summary> 15 /// 面試狀態 16 /// </summary> 17 public string state { get; set; } 18 public GCZ_abstract gcz { get; set; }//面試者 19 private List<GCZ_abstract> works = new List<GCZ_abstract>();//所有面試人員 20 /// <summary> 21 /// 添加面試人員 22 /// </summary> 23 /// <param name="gcz"></param> 24 public void Add(GCZ_abstract gcz) 25 { 26 works.Add(gcz); 27 } 28 /// <summary> 29 /// 刪除面試人員 30 /// </summary> 31 /// <param name="gcz"></param> 32 public void Remove(GCZ_abstract gcz) 33 { 34 works.Remove(gcz); 35 } 36 public void ShowMessage() 37 { 38 Console.WriteLine("歡迎來面試,我出題了。。。\n"); 39 } 40 41 /// <summary> 42 /// 面試結果 43 /// </summary> 44 public void Result() 45 { 46 foreach(GCZ_abstract man in works) 47 { 48 man.GX(); 49 } 50 } 51 } 52 } View Code訂閱類型
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 面試人員集合 11 /// </summary> 12 public abstract class GCZ_abstract 13 { 14 /// <summary> 15 /// 面試感想 16 /// </summary> 17 public abstract void GX(); 18 } 19 } View Code類型一(訂閱者)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 具體觀察者類型(面試人員) 11 /// </summary> 12 public class GCZ1 : GCZ_abstract 13 { 14 private MSG msg { get; set; } 15 public GCZ1(MSG msg) 16 { 17 this.msg = msg; 18 } 19 20 public override void GX() 21 { 22 if (msg.state == "1") 23 { 24 Console.WriteLine("面試官來了,很激動!"); 25 } 26 else if (msg.state == "2") 27 { 28 Console.WriteLine("面試官還沒來,很緊張!"); 29 } 30 else 31 Console.WriteLine("面試官結束了,輕松!"); 32 } 33 } 34 } View Code類型二(訂閱者)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 具體觀察者類型(面試人員) 11 /// </summary> 12 public class GCZ2 : GCZ_abstract 13 { 14 private MSG msg { get; set; } 15 public GCZ2(MSG msg) 16 { 17 this.msg = msg; 18 } 19 20 public override void GX() 21 { 22 if (msg.state == "1") 23 { 24 Console.WriteLine("面試官來了,我不動明王!"); 25 } 26 else if (msg.state == "2") 27 { 28 Console.WriteLine("面試官還沒來,我不動明王!"); 29 } 30 else 31 Console.WriteLine("面試結束,我面不改色!"); 32 } 33 } 34 } View Code結果
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 MSG msg = new MSG();//面試的面試官 14 msg.ShowMessage();//面試官出題 15 //面試中 16 GCZ1 one = new GCZ1(msg);//面試者1 17 msg.Add(one); 18 GCZ2 two = new GCZ2(msg);//面試者2 19 msg.Add(two); 20 21 msg.state = "2";//面試狀態 22 msg.Result(); 23 } 24 } 25 } View Code?
轉載于:https://www.cnblogs.com/LiuZhen/p/4330460.html
總結
以上是生活随笔為你收集整理的观察者模式--初学入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单元測试用例概述
- 下一篇: EasyUI DateTimeBox设置