example 2: the publisher-subscriber protocol

8

Upload: gefen

Post on 06-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Example 2: The Publisher-Subscriber Protocol. Have two collaborating participants. Publisher. collaboration PublisherSubscriberProtocol { participant Publisher { import void changeOp(Object[] args); protected Vector subscribers = new Vector(); export void attach(Subscriber subsc) { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Example 2: The Publisher-Subscriber Protocol
Page 2: Example 2: The Publisher-Subscriber Protocol

Example 2: The Publisher-Subscriber Protocol

• Have two collaborating participants

Page 3: Example 2: The Publisher-Subscriber Protocol

Publisher

collaboration PublisherSubscriberProtocol {

participant Publisher {

import void changeOp(Object[] args);

protected Vector subscribers = new Vector();

export void attach(Subscriber subsc) {

subscribers.addElement(subsc);}

export void detach(Subscriber subsc) {

subscribers.removeElement(subsc);}

import-export void changeOp() {

import();

for (int i = 0; i < subscribers.size(); i++)

{((Subscriber)subscribers.elementAt(i)).

newUpdate(this);}}

Page 4: Example 2: The Publisher-Subscriber Protocol

Subscriber

participant Subscriber {

import void subUpdate(Publisher publ);

protected Publisher publ;

export void newUpdate(Publisher aPubl) {

publ = aPubl;

subUpdate(publ);}

}

}

}

Page 5: Example 2: The Publisher-Subscriber Protocol

Classes for deployment

Class Point {

void set(…)

}

class InterestedInPointChange {

void public notifyChange() {

System.out.println("CHANGE ...");

}

}

Page 6: Example 2: The Publisher-Subscriber Protocol

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

}

}

}

Page 7: Example 2: The Publisher-Subscriber Protocol

Deployment 1

Publisher

InterestedInPointChange

Point

Subscriber

changeOp calls newUpdateattach(Subscriber)detach(Subscriber)

subUpdate(Publisher)newUpdate(Publisher)

set

subUpdate(Publisher)notifyChange()

Blue: from adapterRed: expectedreplaced

Page 8: Example 2: The Publisher-Subscriber Protocol

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

}

};

}