Observer Pattern - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Observer Pattern

Description:

... may be set several times but the subscribers are only ... needed because too many observers will be informed, // as the list of observers is shared betw. ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 20
Provided by: karllie
Learn more at: https://www2.ccs.neu.edu
Category:
Tags: observer | pattern

less

Transcript and Presenter's Notes

Title: Observer Pattern


1
Observer 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
  • 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
Johans 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
7
Johans 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 hostadd_a_Printer(Print
er) provided by observeaddWatcher
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 collaborations 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
  • ObsAspectv provided by ObsSharedvh
  • export out addWatcher, out remWatcher,
  • in-out get, in-out set
  • Observer ObsAspect.Watcher
  • export in 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 lt 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
Deployment 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()
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()
Write a Comment
User Comments (0)
About PowerShow.com