经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式-观察者设计模式
来源:cnblogs  作者:冰乐  时间:2019/9/20 9:49:32  对本文有异议

一个对象的动作触发多个对象的行为,通过观察者可以去掉对象的依赖,支持各种自定义和扩展。

观察者模式,还要从那只申请的猫开始说起。

猫叫一声之后触发:

  Baby Cry()、Brother Trun()、Dog Wang()、Father Roar()、Mothor Whisper()、Mouse Run()、Neighbour Awake()、Stealer Hide().......

下面代码是这些触发类:

  1. public class Baby
  2. {
  3. public void Cry()
  4. {
  5. Console.WriteLine("{0} Cry", this.GetType().Name);
  6. }
  7. }
  8. public class Brother
  9. {
  10. public void Turn()
  11. {
  12. Console.WriteLine("{0} Turn", this.GetType().Name);
  13. }
  14. }
  15. public class Chicken
  16. {
  17. public void Woo()
  18. {
  19. Console.WriteLine("{0} Woo", this.GetType().Name);
  20. }
  21. }
  22. public class Dog
  23. {
  24. public void Wang()
  25. {
  26. Console.WriteLine("{0} Wang", this.GetType().Name);
  27. }
  28. }
  29. public class Father
  30. {
  31. public void Roar()
  32. {
  33. Console.WriteLine("{0} Roar", this.GetType().Name);
  34. }
  35. }
  36. public class Mother
  37. {
  38. public void Whisper()
  39. {
  40. Console.WriteLine("{0} Whisper", this.GetType().Name);
  41. }
  42. }
  43. public class Mouse
  44. {
  45. public void Run()
  46. {
  47. Console.WriteLine("{0} Run", this.GetType().Name);
  48. }
  49. }
  50. public class Neighbor
  51. {
  52. public void Awake()
  53. {
  54. Console.WriteLine("{0} Awake", this.GetType().Name);
  55. }
  56. }
  57. public class Stealer
  58. {
  59. public void Hide()
  60. {
  61. Console.WriteLine("{0} Hide", this.GetType().Name);
  62. }
  63. }
View Code

然后,猫叫一声,触发这些动作:

  1. public class Cat
  2. {
  3. public void Miao()
  4. {
  5. Console.WriteLine("{0} Miao.....", this.GetType().Name);
  6. new Mouse().Run();//依赖
  7. new Chicken().Woo();
  8. new Baby().Cry();
  9. new Brother().Turn();
  10. new Dog().Wang();
  11. new Father().Roar();
  12. new Mother().Whisper();
  13. //new Mouse().Run();
  14. new Neighbor().Awake();
  15. //new Stealer().Hide();
  16. }
  17. }

这样写,功能是实现了,但是依赖太多,任何一个对象改动,都会导致Cat类变化,违背了单一职责,不仅自己Miao,还要触发各种动作,不稳定,加一个/减一个/调整顺序,Cat都得改。

Cat职责:

  1、Miao()

  2、触发一些列动作,这其实就是需求

  3、实现上,其实多了一个,指定动作

Cat不稳定的原因,在Miao()方法里面的一些对象。

这个时候就要甩锅大法了,把锅丢出去与,只管自己,把不稳定的地方移出去,自己只写稳定的,这样能保证自身的稳定。

定义一个接口,把这些动作抽象出一个Action(),只是为了把多个对象产生关系,方便保存和调用,方法本身其实没用的:

  1. public interface IObserver
  2. {
  3. void Action();
  4. }

让上面那些Miao一声后需要触发的动作类,实现这个接口:

  1. public class Baby : IObserver
  2. {
  3. public void Action()
  4. {
  5. this.Cry();
  6. }
  7. public void Cry()
  8. {
  9. Console.WriteLine("{0} Cry", this.GetType().Name);
  10. }
  11. }
  12. public class Brother : IObserver
  13. {
  14. public void Action()
  15. {
  16. this.Turn();
  17. }
  18. public void Turn()
  19. {
  20. Console.WriteLine("{0} Turn", this.GetType().Name);
  21. }
  22. }
  23. public class Chicken : IObserver
  24. {
  25. public void Action()
  26. {
  27. this.Woo();
  28. }
  29. public void Woo()
  30. {
  31. Console.WriteLine("{0} Woo", this.GetType().Name);
  32. }
  33. }
  34. public class Dog : IObserver
  35. {
  36. public void Action()
  37. {
  38. this.Wang();
  39. }
  40. public void Wang()
  41. {
  42. Console.WriteLine("{0} Wang", this.GetType().Name);
  43. }
  44. }
  45. public class Father : IObserver
  46. {
  47. public void Action()
  48. {
  49. this.Roar();
  50. }
  51. public void Roar()
  52. {
  53. Console.WriteLine("{0} Roar", this.GetType().Name);
  54. }
  55. }
  56. public class Mother : IObserver
  57. {
  58. public void Action()
  59. {
  60. this.Whisper();
  61. }
  62. public void Whisper()
  63. {
  64. Console.WriteLine("{0} Whisper", this.GetType().Name);
  65. }
  66. }
  67. public class Mouse : IObserver
  68. {
  69. public void Action()
  70. {
  71. this.Run();
  72. }
  73. public void Run()
  74. {
  75. Console.WriteLine("{0} Run", this.GetType().Name);
  76. }
  77. }
  78. public class Neighbor : IObserver
  79. {
  80. public void Action()
  81. {
  82. this.Awake();
  83. }
  84. public void Awake()
  85. {
  86. Console.WriteLine("{0} Awake", this.GetType().Name);
  87. }
  88. }
  89. public class Stealer : IObserver
  90. {
  91. public void Action()
  92. {
  93. this.Hide();
  94. }
  95. public void Hide()
  96. {
  97. Console.WriteLine("{0} Hide", this.GetType().Name);
  98. }
  99. }
View Code

Cat这个类就可以修改成如下,使用集合或者事件都是可以的:

  1. public class Cat
  2. {
  3. //Cat不稳定--这一堆对象--甩锅--自己不写让别人传递
  4. private List<IObserver> _ObserverList = new List<IObserver>();
  5. public void AddObserver(IObserver observer)
  6. {
  7. this._ObserverList.Add(observer);
         MiaoHandler += observer.Action;
  8. }
  9. public void MiaoObserver()
  10. {
  11. Console.WriteLine("{0} MiaoObserver.....", this.GetType().Name);
  12. if (this._ObserverList != null && this._ObserverList.Count > 0)
  13. {
  14. foreach (var item in this._ObserverList)
  15. {
  16. item.Action();
  17. }
  18. }
  19. }
  20. private event Action MiaoHandler;
  21. public void MiaoEvent()
  22. {
  23. Console.WriteLine("{0} MiaoEvent.....", this.GetType().Name);
  24. if (this.MiaoHandler != null)
  25. {
  26. foreach (Action item in this.MiaoHandler.GetInvocationList())
  27. {
  28. item.Invoke();
  29. }
  30. }
  31. }
  32. }

这样在调用的时候,增加一个,减少一个,更改顺序,就不要再去修改Cat类了。

  1. Cat cat = new Cat();
  2. cat.AddObserver(new Chicken());
  3. cat.AddObserver(new Baby());
  4. cat.AddObserver(new Brother());
  5. cat.AddObserver(new Dog());
  6. cat.AddObserver(new Father());
  7. cat.AddObserver(new Mother());
  8. cat.AddObserver(new Mouse());
  9. cat.AddObserver(new Neighbor());
  10. cat.AddObserver(new Stealer());
  11. cat.MiaoObserver();

 

原文链接:http://www.cnblogs.com/taotaozhuanyong/p/11552420.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号