Saturday, October 20, 2007

Observer Pattern (4) Step By Step

From step 3, we can see that the BankAccount class is still subject to change, so in this step, we will abstract the BankAccount a bit more, code change in bold:

public class BankAccount

{

ArrayList<IAccountObserver> observerList = new ArrayList<IAccountObserver>();

public void Withdraw(int data)

{

//...

}

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);

}

}

blog comments powered by Disqus