110 likes | 286 Views
CS 350 – Software Design The Observer Pattern – Chapter 18. Let’s expand the case study to include new features: Sending a welcome letter to new customers Verify the customer’s address with the post office In an ideal world, we know all the requirements and they will not change.
E N D
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers Verify the customer’s address with the post office In an ideal world, we know all the requirements and they will not change. If they don’t change, they can be hard coded into the Customer object as shown below:
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Class Responsibility Customer When a customer is added, this object will make the calls to the other objects to have the corresponding actions take place WelcomeLetter Creates welcome letters for customers that let them know they were added to the system. AddrVerification This object will verify the address of any customer that asks it to
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Gang of Four Definiton: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and update automatically. Note we do not want to change the broadcasting object every time there is a change to the set of objects listening to the broadcast. We want to decouple the notify-ers and the notify-ees. Two things vary Different kinds of objects – ones that need to be notified of a change in state. Different interfaces – Each object that requires notification may have a different interface.
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Objects that must be notified are called Observers. All observers must have the same interface. All Observers must register themselves. This makes them responsible for knowing what they are watching for. This also frees the subject from knowing which observers depend on it. We must add two methods to the subject. attach(Observer) adds the given observer to its list of observers. detatch(Observer) removes the given observer from its list of observers. With the observers registered it is a simple matter for the subject to notify the Observers when an event occurs. The observer must implement a method called update. The subject implements the notify method that goes through its list of Observers and calls this update method for each of them.
CS 350 – Software DesignThe Observer Pattern – Chapter 18 This is not enough. In many cases the Observers need more information than just that the event occurred. To solve this the subject must contain additional methods that can be called by the Observer so that the observer can obtain the necessary information.
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Observer Code public class Customer { static private Vector myObs; static { myObs = new Vector(); } public static void attach(MyObserver 0) { myObs.addElement(o); } public static void detach(MyObserver 0) { myObs.remove(o); } public String getState() { //Add methods that will give required information return null; } public void notifyObs() { //set arg to something that helps tell the Observer what happened for (Enumeration e =myObs.elements(); e.hasMoreElements{} ;) { ((MYObserver) e).update(this); } } }
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Observer Code interface MyObserver { void update (Custom myCust);} class AddrVerification implement MyObserver { public AddrVerification() {} Public void update (Customer myCust) { //do Adddress Verification stuff here }} class WelcomeLetter implements MyObserver { public WelcomeLetter () {}public void update (Customer myCust) { // do Welcome Letter Stuff here }}
CS 350 – Software DesignThe Observer Pattern – Chapter 18 What happens when we add new observers? Imagine adding the ability to send a letter with coupons to customers located within 20 miles of one of the company’s “Brick and Mortar” stores. We just need to add a new observer that send a coupon.
CS 350 – Software DesignThe Observer Pattern – Chapter 18 If the class we want to add already exists and we do not wish to modify it, then we can adapt it as shown below:
CS 350 – Software DesignThe Observer Pattern – Chapter 18 The Observer Pattern: Key Features Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Problem: You need to notify a varying list of objects that an event has occurred. Solution: Observers delegate the responsibility for monitoring for an event to a central object: The subject. Implementation: Have objects (Observers) that want to know when an event happen attach themselves to another object (Subject) that is watching for the event to occur or that triggers the event itself. When the event occurs, the Subject tells the Observers that it has occurred. The Adapter pattern is sometimes needed to be able to implement the Observer interface for all the Observer-type objects.
CS 350 – Software DesignThe Observer Pattern – Chapter 18 Generic Structure Observer Pattern