设计模式学习之路——Observer 观察者模式
观察者模式的效果
Observer促进了目标的抽象耦合,目标不知道任何一个观察者的详细内容。但这也具有潜在的缺点:当目标中的数据发生了一系列的递增变化时,要持续或反复地更新观察者。如果更新的代价很高,就有必要引进某种管理更改的策略,这样就不会多次或频繁地通知观察者。
当一个客户(观察者)对底层数据做了修改,你要决定由哪一个对象去触发送给其他观察者的更新通知。如果是由目标在它被更改后去通知所有的观察者,每个客户就不需要记住去触发通知。但另一方面,这会导致多个连续的更新被触发。如果由客户告诉目标何时去通知其他的客户,会避免这种连续的通知。但是客户增加了告诉目标何时发送通知的责任。如果一个客户“忘记”了,程序就不能正常运行。
最后一点,依据所发生的变化的类型或范围,为观察者定义多个接受通知的更新方法,以此指定要选择发送的通知类型,在有些情况下,客户能由此忽略掉一些更新通知。
动机:
在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” ——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。
使用面向对象技术,可以将这种依赖关系弱化,并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。
意图:
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新
——《设计模式》GoF
结构:
代码实现:
using System;
using System.Collections.Generic;
using System.Text;
namespace Observer
{
class Program
{
static void Main(string[] args)
{
Subject subject = new BankAccount();
IAccountObserver email = new Email();
IAccountObserver tel = new Telphone();
subject.AddObserver(email);
subject.AddObserver(tel);
subject.WithAccount(100);
Console.ReadKey();
}
}
public class UserAccountArgs
{
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
private string mobile;
public string Mobile
{
get { return mobile; }
set { mobile = value; }
}
}
public class Email : IAccountObserver
{
public void Update(UserAccountArgs args)
{
Console.WriteLine(args.Address);
}
}
public class Telphone : IAccountObserver
{
public void Update(UserAccountArgs args)
{
Console.WriteLine(args.Mobile);
}
}
public interface IAccountObserver
{
void Update(UserAccountArgs args);
}
public class BankAccount : Subject
{
public override void WithAccount(int data)
{
UserAccountArgs args = new UserAccountArgs();
args.Address = "邮件通知账户金额为:" + data.ToString();
args.Mobile = "短信通知账户金额为:" + data.ToString();
Notify(args);
}
}
public abstract class Subject
{
List<IAccountObserver> observerList = new List<IAccountObserver>();
protected virtual void Notify(UserAccountArgs args)
{
foreach (IAccountObserver observer in observerList)
{
observer.Update(args);
}
}
public void AddObserver(IAccountObserver observer)
{
observerList.Add(observer);
}
public void RemoveObserver(IAccountObserver observer)
{
observerList.Remove(observer);
}
public abstract void WithAccount(int data);
}
}
.NET框架中的Observer模式(事件 Events)
using System;
using System.Collections.Generic;
using System.Text;
namespace ObserverEvent
{
class Program
{
static void Main(string[] args)
{
BankAccount bankAccount = new BankAccount();
Emailer emailer = new Emailer();
Mobile mobiler = new Mobile();
bankAccount.AccountChange += new AccountChangeEventHandler(emailer.Update);
bankAccount.AccountChange += new AccountChangeEventHandler(mobiler.Update);
bankAccount.WithAccount(100);
Console.ReadKey();
}
}
public class UserAccountEventArgs : EventArgs
{
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
private string mobile;
public string Mobile
{
get { return mobile; }
set { mobile = value; }
}
}
public delegate void AccountChangeEventHandler(object sender, UserAccountEventArgs e);
public class BankAccount
{
public event AccountChangeEventHandler AccountChange;
protected virtual void OnAccountChange(UserAccountEventArgs e)
{
if (this.AccountChange != null)
{
this.AccountChange(this, e);
}
}
public void WithAccount(int data)
{
UserAccountEventArgs args = new UserAccountEventArgs();
args.Address = "邮件通知账户金额为:" + data.ToString();
args.Mobile = "短信通知账户金额为:" + data.ToString();
OnAccountChange(args);
}
}
public class Emailer
{
public void Update(object sender, UserAccountEventArgs e)
{
Console.WriteLine(e.Address);
}
}
public class Mobile
{
public void Update(object sender, UserAccountEventArgs e)
{
Console.WriteLine(e.Mobile);
}
}
}
Observer模式的几个要点
1.使用面向对象的抽象,Observer模式使得我们可以独立地改变目标与观察者,从而使二者之间的依赖关系达致松耦合。
2.目标发送通知时,无需指定观察者,通知(可以携带通知信息作为参数)会自动传播。观察者自己决定是否需要订阅通知,目标对象对此一无所知。
3.在C#的event中,委托充当了抽象的Observer接口,而提供事件的对象充当了目标对象。委托是比抽象Observer接口更为松耦合的设计。
浙公网安备 33010602011771号