Title: Observer Pattern
1Observer Pattern
- Only inform subscriber before the publishers
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 3Collaborations
- encapsulate several roles into a behavioral unit
of reuse - is one aspectual scope?
- an aspectual group
4Aspectual Methods
- Aspectual methods may be attached to several
methods. Aspectual group. - changeOp is played by set
- in-out methods.
5Attachment
- attach methods
- attach classes (roles to classes)
- adapter PubSubConn1
- Publisher is played by Point
- changeOp is played by set
6Johans 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 iv.iterator() i.hasNext())
(Watcher) i.next().gotten(this) changed
false return r
7Johans 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)
8Small collaboration
- collaboration ObsShared
- role CollectionHolder
- Collection vh new Vector()
-
-
9Bind host class
adapter AddObserver runnable host,
observe scope host.Vars
observe.Observed hostadd_a_Printer(Print
er) provided by observeaddWatcher
10Now 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 collaborations roles, the
adapter also specifies the exportation back to
the host application.
11What is the host?
- adapter ObsCombined
- ObsAspect, ObsShared
- combine two collaborations to get an adapter?
Overloading the adapter concept?
12Combining collaborations
- adapter ObsCombined
- ObsAspect, ObsShared
- Observed ObsAspect.Watched,
ObsShared.VectorHolder - ObsAspectv provided by ObsSharedvh
- export out addWatcher, out remWatcher,
- in-out get, in-out set
-
- Observer ObsAspect.Watcher
- export in inform
-
-
13Example 2 The Publisher-Subscriber Protocol
- Have two collaborating participants
14Publisher
- 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 lt subscribers.size()
i) - ((Subscriber)subscribers.elementAt(i)
). - newUpdate(this)
15Subscriber
- participant Subscriber
- in void subUpdate(Publisher publ)
- protected Publisher publ
- out void newUpdate(Publisher aPubl)
- publ aPubl
- subUpdate(publ)
-
-
-
16Classes for deployment
- Class Point
- void set()
-
- class InterestedInPointChange
- void public notifyChange()
- System.out.println("CHANGE ...")
-
-
17Deployment 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())
-
-
-
18Deployment 1
Red expected replaced
Blue from adapter
Point
Publisher
changeOp calls newUpdate attach(Subscriber) deta
ch(Subscriber)
set
InterestedInPointChange
Subscriber
subUpdate(Publisher) newUpdate(Publisher)
subUpdate(Publisher) notifyChange()
19Deployment 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()
-
-
-