觀察者模式(Observer Pattern)
觀察者模式(Observer Pattern)是一種對象行為模式。它定義對象間的一種一對多的依賴關系,當一個對象的狀態(tài)發(fā)生改變時,所有依賴于它的對象都得到通知并被自動更新。在觀察者模式中,主體是通知的發(fā)布者,它發(fā)出通知時并不需要知道誰是它的觀察者,可以有任意數(shù)目的觀察者訂閱并接收通知。觀察者模式不僅被廣泛應用于軟件界面元素之間的交互,在業(yè)務對象之間的交互、權限管理等方面也有廣泛的應用。觀察者模式的主要的作用就是對對象解耦,將觀察者和被觀察者完全隔離。
1)面向對象方式實現(xiàn)
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
//一般每個接口或類都寫在單獨的.cs文件中
//本示例為了執(zhí)行查看方便才寫在一起
///
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
{
//一般的實現(xiàn)
//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)事件委托方式實現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-562361.html
using System;
using System.Collections.Generic;
namespace ConsoleApplianimalion
{
//一般每個接口或類都寫在單獨的.cs文件中
//本示例為了執(zhí)行查看方便才寫在一起
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
{
//一般的實現(xiàn)
//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();
}
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-562361.html
到了這里,關于C#設計模式之---觀察者模式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!