理解不是很深,不做解说,自己写了个小例子记录下来
例子以面试者去面试为主题,容易理解,例子简单(小白写的,能不简单吗),能看的懂的代码才是好代码啊
观察者接口
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 }
观察者
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 }
订阅类型
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 }
类型一(订阅者)
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 }
类型二(订阅者)
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 }
结果
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 }