The Observer Design Pattern allows you to have multiple ...
14. The Observer Design Pattern allows you to have multiple current views of the same object by showing the state of the observed object every time its state changes. One example is when a spreadsheet has several views of the same list of numbers. Whenever the list of numbers changes, so will both charts that are "observing" the model:
| |5 | | | | | | |
| |6 | | | | | | |
| | | | | | | | |
| |7 | | | | | | |
| | | | | | | | |
| | | | | | | | |
| |3 | | | | | | |
| |5 | | | | | | |
| |10 | | | | | | |
This question asks you to resolve a simpler problem. The code below changes the state of the observed object (m1) during a setValue message. At that time, m1 notifies all added Observer objects (v1 and v2) to show the toString version of m1. The Model class must have a toString that does this silly thing: Return a string with its stored value (ints such as 1, 2, and 4) raised to the same value using Math.pow(value, value)).
|Model m1 = new Model(); |[pic] |
|ViewOne v1 = new ViewOne(); | |
|ViewTwo v2 = new ViewTwo(); |[pic] |
| | |
|m1.addObserver(v1); |[pic] |
|m1.addObserver(v2); | |
| | |
|m1.setValue(1); // notify observers | |
|m1.setValue(2); | |
|m1.setValue(4); | |
| | |
| | |
|Output to the console occurs at the same time | |
|the dialogs to the right are shown. | |
| | |
|class ViewOne: | |
|1 to the 1 power = 1.0 | |
| | |
|class ViewOne: | |
|2 to the 2 power = 4.0 | |
| | |
|class ViewOne: | |
|4 to the 4 power = 256.0 | |
In the design shown on the next page (see UML class diagram), each Observer object (v1 and v2) uses its own updateTheView method to show the current state of the observed object (m1). This is made possible by giving each Observer object (v1 and v2) access to the observed object m1. ViewOne must print the toString value at the same time ViewTwo displays the toString value in a JOptionPane showMessageDialog.
Answer the four questions on the next two pages using the UML diagram representing this particular example of Observer. Note: The parameters and fields were omitted on purpose. This picture on the next page used is Rational Software Architect's UML tool where the circles mean public and the one square (before notifyObservers) means private.
[pic]
Assume all imports exist. Do not take the time to import anything
a) Write interface Observer.
b) Write class ViewOne so it will print "class ViewOne:" followed by the toString value each time the Model object sends a ViewOne object an updateTheView message.
c) Write class ViewTwo so it will display "class ViewTwo:" followed by the toString value of the Model object in a JOptionPane.showMessageDialogBox each time the Model object sends a ViewTwo object an updateTheView message.
d) Write class Model. Ensure it can send the correct message at the correct time to all Observer objects that have been added, even if there are three or four. Do not allow class cast exceptions. Make sure only Observer objects can be added.
Answers
11. ViewOne
public class ViewOne implements Observer {
public void updateTheView(Object theObservedObject) {
System.out.println(this.getClass() + ":\n" + theObservedObject.toString());
}
}
+2 Class heading
+1 has ViewOne
+1 implements Observer
+2 Method heading
+1 has updateTheView
+1 has parameter
+1 prints something.toString()
12. ViewTwo
public class ViewTwo implements Observer {
public void updateTheView(Object theObservedObject) {
JOptionPane.showMessageDialog(null, "class ViewTwo" + ":\n"
+theObservedObject.toString());
}
}
+2 Class heading
+1 has ViewTwo
+1 implements Observer
+2 Method heading
+1 has updateTheView
+1 has parameter
+1 calls JoptionPane.showMessageDialog with something.toString()
13. Class Model:
public class Model {
private ArrayList observers;
private int value;
public Model() {
observers = new ArrayList();
}
public void addObserver(Observer anObserver) {
observers.add(anObserver);
}
private void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer o = (Observer) observers.get(i);
o.updateTheView(this);
}
}
public String toString() {
return "" + value + " to the " + value + " power = "
+ Math.pow(value, value);
}
public void setValue(int value) {
this.value = value;
notifyObservers();
}
}
|+2 Has a collection to add observers |+6 notifyObservers |
|+1 has an int value |+1 heading |
| |+2 loops through observers |
|+2 addObserver |+2 sends updateTheView messages |
|+1 heading |+1 passes this |
|+1 adds observer | |
| |+4 setValue |
|+2 toString |+1 heading |
|+1 heading |+1 changes value |
|+1 Math.pow(val,val) |+2 notifiesObserver |
| |+1 Other stuff |
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- why you should have a will
- do you have to have a will
- have to have it synonym
- men who have multiple affairs
- clips online the microsoft design gallery
- here s how much you should have saved
- what causes the earth to have seasons
- why you no have boyfriend
- can you say have had
- thank you to the staff
- seems to have the same effect
- why men have multiple affairs