观察者模式(Observer Pattern)
观察者模式(Observer Pattern)是一种对象行为模式。它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。在观察者模式中,主体是通知的发布者,它发出通知时并不需要知道谁是它的观察者,可以有任意数目的观察者订阅并接收通知。观察者模式不仅被广泛应用于软件界面元素之间的交互,在业务对象之间的交互、权限管理等方面也有广泛的应用。观察者模式的主要的作用就是对对象解耦,将观察者和被观察者完全隔离。
1)面向对象方式实现
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{//一般每个接口或类都写在单独的.cs文件中//本示例为了执行查看方便才写在一起 /// public interface IObserver{void Action();}public class Baby : IObserver{public void Action(){this.Cry();}public void Cry(){Console.WriteLine("{0} Cry", this.GetType().Name);}}public class Brother : IObserver{public void Action(){this.Turn();}public void Turn(){Console.WriteLine("{0} Turn", this.GetType().Name);}}public class Chicken : IObserver{public void Action(){this.Woo();}public void Woo(){Console.WriteLine("{0} Woo", this.GetType().Name);}}public class Dog : IObserver{public void Action(){this.Wang();}public void Wang(){Console.WriteLine("{0} Wang", this.GetType().Name);}}public class Neighbor : IObserver{public void Action(){this.Awake();}public void Awake(){Console.WriteLine("{0} Awake", this.GetType().Name);}}public class Animal{//一般的实现//public void Sound()//{// Console.WriteLine("{0} Sound.....", this.GetType().Name);// new Chicken().Woo();// new Baby().Cry();// new Brother().Turn();// new Dog().Wang();// new Neighbor().Awake();//}private List<IObserver> _ObserverList = new List<IObserver>();public void Add(IObserver observer){this._ObserverList.Add(observer);}public void Remove(IObserver observer){this._ObserverList.Remove(observer);}public void SoundObserver(){Console.WriteLine("{0} SoundObserver.....", this.GetType().Name);foreach (var observer in this._ObserverList){observer.Action();}}}// /// 观察者模式/// 对象和行为的分离/// class Program{static void Main(string[] args){Animal animal = new Animal();animal.Add(new Baby());animal.Add(new Brother());animal.Add(new Chicken());animal.Add(new Dog());animal.Add(new Neighbor());animal.SoundObserver();}}
}
2)事件委托方式实现
using System;
using System.Collections.Generic;
namespace ConsoleApplianimalion
{//一般每个接口或类都写在单独的.cs文件中//本示例为了执行查看方便才写在一起 public interface IObserver{void Action();}public class Baby : IObserver{public void Action(){this.Cry();}public void Cry(){Console.WriteLine("{0} Cry", this.GetType().Name);}}public class Brother : IObserver{public void Action(){this.Turn();}public void Turn(){Console.WriteLine("{0} Turn", this.GetType().Name);}}public class Chicken : IObserver{public void Action(){this.Woo();}public void Woo(){Console.WriteLine("{0} Woo", this.GetType().Name);}}public class Dog : IObserver{public void Action(){this.Wang();}public void Wang(){Console.WriteLine("{0} Wang", this.GetType().Name);}}public class Neighbor : IObserver{public void Action(){this.Awake();}public void Awake(){Console.WriteLine("{0} Awake", this.GetType().Name);}}public class Animal{//一般的实现//public void Sound()//{// Console.WriteLine("{0} Sound.....", this.GetType().Name);// new Chicken().Woo();// new Baby().Cry();// new Brother().Turn();// new Dog().Wang();// new Neighbor().Awake();//}public event Action SoundHandler;public void SoundEvent(){Console.WriteLine("{0} SoundEvent.....", this.GetType().Name);if (this.SoundHandler != null){//foreach (Action action in this.SoundHandler.GetInvoanimalionList())//{// action.Invoke();//}this.SoundHandler.Invoke();}}}// /// 观察者模式/// 对象和行为的分离/// class Program{static void Main(string[] args){Animal animal = new Animal();animal.SoundHandler += new Action(() => new Dog().Wang());animal.SoundHandler += new Chicken().Woo;animal.SoundHandler += new Baby().Cry;animal.SoundHandler += new Brother().Turn;animal.SoundHandler += new Neighbor().Awake;animal.SoundEvent();}}
}