90 likes | 219 Views
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) {
E N D
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()); } } }
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()
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(); } }; }