1 / 19

Observer Pattern

Observer Pattern. Only inform subscriber before the publisher’s value is retrieved. The publisher may be set several times but the subscribers are only informed before the first get after a change. To demonstrate sharing, we want the list of subscribers to be on a per-publisher basis.

maia
Download Presentation

Observer Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Observer Pattern • Only inform subscriber before the publisher’s value is retrieved. The publisher may be set several times but the subscribers are only informed before the first get after a change. • To demonstrate sharing, we want the list of subscribers to be on a per-publisher basis. • The flag that reports change is shared by all subscribers.

  2. per attachment basis?

  3. Collaborations • encapsulate several roles into a behavioral unit of reuse • is one aspectual scope? • an aspectual group

  4. Aspectual Methods • Aspectual methods may be attached to several methods. Aspectual group. • { changeOp is played by set*;} • in-out methods.

  5. Attachment • attach methods • attach classes (roles to classes) adapter PubSubConn1 { Publisher is played by Point { changeOp is played by set*;}

  6. Johan’s Example collaboration ObsAspect role Watched { boolean changed = true; in Collection v; out addWatcher(Watcher w) {v.addElement(w);} out remWatcher(Watcher w) {v.removeElement(w);} in-out ResultA set(Method e) { ResultA r = e.invoke(); changed = true; return r; } in-out ResultB get(Method e) { ResultB r = e.invoke(); if (changed) { for (Iterator i=v.iterator(); i.hasNext();) (Watcher) i.next().gotten(this); changed = false;} return r; } }

  7. Johan’s Example (continued) collaboration ObsAspect role Watcher { void gotten(Watched){ if (w.changed) inform(w); // needed because too many observers will be informed, // as the list of observers is shared betw. attachments in void inform(Watched w); } }

  8. Small collaboration collaboration ObsShared { role CollectionHolder { Collection vh = new Vector(); } }

  9. Bind host class adapter AddObserver { runnable = {host, observe} { scope { host.Vars += observe.Observed { host::add_a_Printer(Printer) provided by observe::addWatcher

  10. Now combine the two.Is this the task of an adapter? • The semantic purpose of an adapter is to reconcile the interface a collaboration requires with the behavior provided by the host. • In addition to specifying how the host fills in the holes of the collaboration’s roles, the adapter also specifies the exportation back to the host application.

  11. What is the host? adapter ObsCombined = {ObsAspect, ObsShared} {…} • combine two collaborations to get an adapter? Overloading the adapter concept?

  12. Combining collaborations adapter ObsCombined = {ObsAspect, ObsShared} { Observed = {ObsAspect.Watched, ObsShared.VectorHolder} { ObsAspect::v provided by ObsShared::vh; exportout addWatcher, out remWatcher, in-out get, in-out set } Observer = {ObsAspect.Watcher} { exportin inform; } }

  13. Example 2: The Publisher-Subscriber Protocol • Have two collaborating participants

  14. Publisher collaboration PublisherSubscriberProtocol { participant Publisher { protected Vector subscribers = new Vector(); out void attach(Subscriber subsc) { subscribers.addElement(subsc);} out void detach(Subscriber subsc) { subscribers.removeElement(subsc);} in-out void changeOp(*) { imported(*); for (int i = 0; i < subscribers.size(); i++) {((Subscriber)subscribers.elementAt(i)). newUpdate(this);}}

  15. Subscriber participant Subscriber { in void subUpdate(Publisher publ); protected Publisher publ; out void newUpdate(Publisher aPubl) { publ = aPubl; subUpdate(publ);} } } }

  16. Classes for deployment Class Point { void set(…) } class InterestedInPointChange { void public notifyChange() { System.out.println("CHANGE ..."); } }

  17. Deployment 1 adapter PubSubConn1 { Publisher is played by Point { changeOp is played by set*;} Subscriber is played by InterestedInPointChange { void subUpdate(Publisher publ) { notifyChange(); System.out.println(”on Point object " + ((Point) publ).toString()); } } }

  18. Red: expected replaced Blue: from adapter Deployment 1 Point Publisher changeOp calls newUpdate attach(Subscriber) detach(Subscriber) set InterestedInPointChange Subscriber subUpdate(Publisher) newUpdate(Publisher) subUpdate(Publisher) notifyChange()

  19. Deployment 2 adapter PubSubConn2 { Publisher is played by TicTacToe { changeOp is played by {startGame, newPlayer, putMark, endGame}}; Subscriber is played by {BoardDisplay, StatusDisplay}{ void subUpdate(Publisher publ) { setGame((Game) publ); repaint(); } }; }

More Related