example 2: the publisher-subscriber protocol
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 PresentationTRANSCRIPT
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) {
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);}}
Subscriber
participant Subscriber {
import void subUpdate(Publisher publ);
protected Publisher publ;
export void newUpdate(Publisher aPubl) {
publ = aPubl;
subUpdate(publ);}
}
}
}
Classes for deployment
Class Point {
void set(…)
}
class InterestedInPointChange {
void public notifyChange() {
System.out.println("CHANGE ...");
}
}
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());
}
}
}
Deployment 1
Publisher
InterestedInPointChange
Point
Subscriber
changeOp calls newUpdateattach(Subscriber)detach(Subscriber)
subUpdate(Publisher)newUpdate(Publisher)
set
subUpdate(Publisher)notifyChange()
Blue: from adapterRed: expectedreplaced
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();
}
};
}