This document describes an application that uses the Observer design pattern. It defines a SpecialSubject class that extends an observable date class and can have its name and price fields updated. When these fields change, observers are notified via a delegated observable object. It also includes a NameObserver class that implements the Observer interface to print out updates to the name field.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
18 views
Template For The Use Case
This document describes an application that uses the Observer design pattern. It defines a SpecialSubject class that extends an observable date class and can have its name and price fields updated. When these fields change, observers are notified via a delegated observable object. It also includes a NameObserver class that implements the Observer interface to print out updates to the name field.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
Template for the Use Case
Domain Model for the toy restaurant system
Class Diagram for the toy restaurant system
Architecture of the composite pattern
An application and related classes using the observer pattern
public class SpecialSubject extends MyDate { private String name; private float price; private DelegatedObservable obs; public SpecialSubject(String name, float price) { this.name = name; this.price = price; obs = new DelegatedObservable(); } public String getName() { return name; } public float getPrice() { return price; } public Observable getObservable() { return obs; }
public void setName(String name) {
this.name = name; obs.setChanged(); obs.notifyObservers(name); } public void setPrice(float price) { this.price = price; obs.setChanged(); obs.notifyObservers(new Float(price)); } } public class DelegatedObservable extends Observable { public void clearChanged() { super.clearChanged(); } public void setChanged() { super.setChanged(); } } import java.util.Observer; public class NameObserver implements Observer { private String name; public NameObserver() { name = null; System.out.println("NameObserver created: Name is " + name); } /* The method update is called whenever the observed object is changed. An application calls an Observable object's notifyObservers method to have all the object's observers notified of the change. */ public void update(Observable obj, Object arg) { if (arg instanceof String) { name = (String) arg; System.out.println("NameObserver: Name changed to " + name); } else { System.out.println("NameObserver: Some other change to subject!"); } } }