Using the UML Drawing
Tool by implementing the code in Java demonstrate the Observer
Design Pattern.
The Publisher-Subscriber design pattern
helps to keep the state of cooperating components synchronized. To achieve this
it enables one-way propagation of changes: one publisher notifies any number of
subscribers about changes to its state.
Structure:
Participant Classes:
Subject: Keeps track of its observers. Provides an
interface for attaching and detaching Observer objects
Observer: Defines an interface for update notification
ConcreteSubject: The object being observed. stores state of interest to
ConcreteObserver objects. Sends a notification to its observers when its state
changes
ConcreteObserver:
The observing object. Stores
state that should stay consistent with the subject's. Implements the Observer
update interface to keep its state consistent with the ConcreteSubject
Problem Statement:
A situation often arises in which data changes in one place, but many other components depend on this
data. The classical example is user
interface elements: when some
internal data element changes all views that depend on this data have to be
updated. We could solve the problem by
introducing direct calling dependencies
along which to propagate the
changes, but this
solution is inflexible
and not reusable. We are looking
for a more general change-propagation mechanism that is applicable in many
contexts.
Solution:
One dedicated component takes the role of the publisher (called subject). All components dependent on changes in
the publisher are its subscribers
(called observers).
Source code
import
java.util.Observable;
import
java.util.Observer;
import java.io.*;
class Publisher
extends Observable
{
private String
name;
public
Publisher(String name)
{
this.name=name;
System.out.println("Subject
is Created with the name "+name);
}
public void
setobserver()
{
BufferedReader
br = new BufferedReader(new InputStreamReader( System.in));
System.out.print("Enter new
name> ");
try
{
name
= br.readLine();
setChanged();
notifyObservers(name);
}
catch(Exception e)
{
}
}
}
class Subscriber1
implements Observer
{
private String resp;
public void update (Observable obj, Object
arg)
{
if (arg instanceof String)
{
resp = (String) arg;
System.out.println("\nReceived
Response in Subscriber1 and name changed to "+resp);
}
}
}
class Subscriber2
implements Observer
{
private String resp;
public void update (Observable obj, Object
arg)
{
if (arg instanceof String)
{
resp = (String) arg;
System.out.println("\nReceived
Response in Subscriber2 and name changed to "+resp);
}
}
}
public class
Publisher_Subscriber
{
public static void main(String args[])
{
Publisher pub = new
Publisher("Summer");
Subscriber1 sub1 = new Subscriber1();
Subscriber2 sub2 = new Subscriber2();
pub.addObserver(sub1);
pub.addObserver(sub2);
pub.setobserver();
}
}
Output
Subject is Created
with the name Summer
Enter new name>
Winter
Received Response
in Subscriber2 and name changed to Winter
Received Response
in Subscriber1 and name changed to Winter
No comments:
Post a Comment