The overall class structure is like this:
- Subject class is the Subject
- BankAccount class is the ConcreteSubject
- IAccountObserver is the observer
- Emailer & Mobile classes are the ConcreteObserver
public abstract class Subject
{
ArrayList<IAccountObserver> observerList = new ArrayList<IAccountObserver>();
public 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 class BankAccount : Subject
{
public void Withdraw(int data)
{
//...
UserAccoutArgs args = new UserAccountArgs();
//...
Notify(args);
}
}