Observer观察者模式
银行帐户改变的时候需要通过短信,邮件等方式通知客户,编写代码实现:
传统的做法:
传统做法
public class ATM
{
BankAccount bankAccount;
void process()
{
bankAccount.Withdraw(data);
}
}
public class BankAccount
{
Emailer emailer;//强依赖关系
Mobile mobile; //强依赖关系
public void Withdraw(int data)
{
//
emailer.SendEmail(userEmail);
mobile.SendNotification(phonenumber);
}
}
public class Emailer
{
public void SendEmail(string to)
{
//..
}
}
public class Mobile
{
public void SendNotification(string to)
{
//..
}
}
public class ATM
{
BankAccount bankAccount;
void process()
{
bankAccount.Withdraw(data);
}
}
public class BankAccount
{
Emailer emailer;//强依赖关系
Mobile mobile; //强依赖关系
public void Withdraw(int data)
{
//
emailer.SendEmail(userEmail);
mobile.SendNotification(phonenumber);
}
}
public class Emailer
{
public void SendEmail(string to)
{
//..
}
}
public class Mobile
{
public void SendNotification(string to)
{
//..
}
}
Observer模式:
Observer
using System.Collections;
public abstract class Subject
{
ArrayList<IAccountObserver> observerList = new ArrayList<IAccountObserver>();
protected virtual void Notifi(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 interface IAccountObserver
{
void Update(UserAccountArgs args);
}
public class BankAccount : Subject
{
public void Withdraw(int data)
{
//
UserAccountArgs args = new UserAccountArgs();
//
Notifi(args);
}
}
public class Emailer : IAccountObserver
{
public void Update(UserAccountArgs args)
{
//.
string toAddress = args.ToAddress;
}
}
public class Mobile : IAccountObserver
{
public void Update(UserAccountArgs args)
{
string mobileNumber = args.MoblieNumber;
}
}
class App
{
public static void Main()
{
Subject subject = new BankAccount();
IAccountObserver observer = new Emailer();
subject.AddObserver(observer);
subject.Withdraw(234);
}
}
using System.Collections;
public abstract class Subject
{
ArrayList<IAccountObserver> observerList = new ArrayList<IAccountObserver>();
protected virtual void Notifi(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 interface IAccountObserver
{
void Update(UserAccountArgs args);
}
public class BankAccount : Subject
{
public void Withdraw(int data)
{
//
UserAccountArgs args = new UserAccountArgs();
//
Notifi(args);
}
}
public class Emailer : IAccountObserver
{
public void Update(UserAccountArgs args)
{
//.
string toAddress = args.ToAddress;
}
}
public class Mobile : IAccountObserver
{
public void Update(UserAccountArgs args)
{
string mobileNumber = args.MoblieNumber;
}
}
class App
{
public static void Main()
{
Subject subject = new BankAccount();
IAccountObserver observer = new Emailer();
subject.AddObserver(observer);
subject.Withdraw(234);
}
}
更牛*的使用委托:
delegate
public delegate void AccountChangeEventHandler(
object sender,
AccountChangeEventArgs args);//接口,约定
public class BankAccount
{
public event AccountChangeEventHandler AccountChange;
public void Withdraw(int data)
{
//
UserAccountArgs args = new UserAccountArgs();
//
Notifi(args);
}
protected virtual void OnAccountChange()
{
if(AccountChange!=null)
{
AccountChange(args);
}
}
}
public class Emailer
{
public void Update(object sender, UserAccountArgs args)
{
//.
string toAddress = args.ToAddress;
}
}
class App
{
public static void Main()
{
BankAccount bankAccount = new BankAccount();
Emailer emailer = new Emailer();
bankAccount.AccountChange +=new AccountChangeEventHandler(emailer.Update);
bankAccount.Withdraw(234);
}
}
public delegate void AccountChangeEventHandler(
object sender,
AccountChangeEventArgs args);//接口,约定
public class BankAccount
{
public event AccountChangeEventHandler AccountChange;
public void Withdraw(int data)
{
//
UserAccountArgs args = new UserAccountArgs();
//
Notifi(args);
}
protected virtual void OnAccountChange()
{
if(AccountChange!=null)
{
AccountChange(args);
}
}
}
public class Emailer
{
public void Update(object sender, UserAccountArgs args)
{
//.
string toAddress = args.ToAddress;
}
}
class App
{
public static void Main()
{
BankAccount bankAccount = new BankAccount();
Emailer emailer = new Emailer();
bankAccount.AccountChange +=new AccountChangeEventHandler(emailer.Update);
bankAccount.Withdraw(234);
}
}