08_BehavioralPatterns.ppt
-
Upload
ernestohp7 -
Category
Documents
-
view
212 -
download
0
Transcript of 08_BehavioralPatterns.ppt
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 1/31
1
Behavioral Patterns
Algorithms and the assignment of responsibilities amongobjects
Describe not just patterns of objects or classes but alsothe patterns of communication between them
Shift your focus away from flow of control to let youconcentrate just on the way objects are interconnected
Observer
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 2/31
2
Behavioral Patterns - Observer Define a one-to-many dependency between objects so that when
one object changes state, all its dependents are notified and
updated automatically.
Intent
An abstraction has two aspects, one dependent on the other.
When changing one object requires changing others, and you don‟t
know how many objects need changed. When an object needs to notify others without knowledge about who
they are.
Applicability
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 3/31
3
Behavioral Patterns - Observer An Example
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 4/31
4
Behavioral Patterns - Observer Strucuture: Class Diagram
subject
observers
*
update()
ConcreteObserver
attach( observer ) detach( observer ) notify()
Subject
for all o in observers o.update()
getState()
subjectState ConcreteSubject
update()
<<interface>> Observer
observerState :=
subject.getState()
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 5/31
5
Behavioral Patterns - Observer Subject
Knows its observers, but not their “real” identity.
Provides an interface for attaching/detaching observers.
Observer
Defines an updating interface for objects that should be identified of changes.
ConcreteSubject
Stores state of interest to ConcreteObserver objects.
Sends update notice to observers upon state change.
ConcreteObserver
Maintains reference to ConcreteSubject (sometimes).
Maintains state that must be consistent with ConcreteSubject. Implements the Observer interface.
Participants
ConcreteSubject notifies observers when changes occur.
ConcreteObserver may query subject regarding state change.
Collaborations
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 6/31
6
Behavioral Patterns - Observer Sequence Diagram
subject : ConcreteSubject
observer1 : ConcreteObserver
observer2 : ConcreteObserver
attach( observer1 )
attach( observer2 )
update()
getState()
update()
getState()
notify()
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 7/31
7
Behavioral Patterns - Observer Benefits
Abstract coupling between Subject and Observer Can reuse subjects without reusing their observers and vice versa
Observers can be added without modifying the subject
All subject knows is its list of observers
Subject does not need to know the concrete class of an observer, just thateach observer implements the update interface
Subject and observer can belong to different abstraction layers
Support for broadcast communication: subject sends notificationto all subscribed observers Observers can be added/removed at any time
Liabilities Possible cascading of notifications
Observers are not necessarily aware of each other and must be carefulabout triggering updates
Simple update interface requires observers to deduce changed item
Consequences
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 8/31
8
Behavioral Patterns - Observer How does the subject keep track of its observers?
Array, linked list
What if an observer wants to observe more than onesubject? Have the subject tell the observer who it is via the update interface
Who triggers the update? The subject whenever its state changes
The observers after they cause one or more state changes
Some third party object(s)
Make sure the subject updates its state before sending out
notifications How much info about the change should the subject send
to the observers? Push Model - Lots
Pull Model - Very Little
Implementation
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 9/31
9
Behavioral Patterns - Observer Can the observers subscribe to specific events of interest?
If so, it's publish-subscribe
Can an observer also be a subject?
What if an observer wants to be notified only after severalsubjects have changed state? Use an intermediary object which acts as a mediator
Subjects send notifications to the mediator object which performs anynecessary processing before notifying the observers
Implementation
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 10/31
10
Behavioral Patterns - Observer MVC Example 1
This example shows the model and the view in the same class/*** Class CounterGui demonstrates having the model and view* in the same class.*/public class CounterGui extends Frame {// The counter. (The model!)private int counter = 0;// The view.private TextField tf = new TextField(10);
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 11/31
11
Behavioral Patterns - Observer MVC Example 1
public CounterGui(String title) {
super(title);Panel tfPanel = new Panel();tf.setText("0");tfPanel.add(tf);
add("North", tfPanel);Panel buttonPanel = new Panel();Button incButton = new Button("Increment");incButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {counter++;tf.setText(counter + "");
}
} );buttonPanel.add(incButton);
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 12/31
12
Behavioral Patterns - Observer MVC Example 1
Button decButton = new Button("Decrement");decButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {counter--;tf.setText(counter + "");}
} );
buttonPanel.add(decButton);Button exitButton = new Button("Exit");exitButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}
} );buttonPanel.add(exitButton);add("South", buttonPanel);
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 13/31
13
Behavioral Patterns - Observer MVC Example 1
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvente) {System.exit(0);}} );
}
public static void main(String[] argv) {CounterGui cg = newCounterGui("CounterGui");cg.setSize(300, 100);cg.setVisible(true);
}}
Where is the controller in this example?The controllers are the instances of the anonymousclasses which handle the button presses.
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 14/31
14
Behavioral Patterns - Observer MVC Example 2
This example shows the model and the view in separate classes
First the view class:/*** Class CounterView demonstrates having the model and view
* in the separate classes. This class is just the view.*/public class CounterView extends Frame {// The view.private TextField tf = new TextField(10);// A reference to our associated model.
private Counter counter;
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 15/31
15
Behavioral Patterns - Observer MVC Example 2
public CounterView(String title, Counter c) {super(title);counter = c;
Panel tfPanel = new Panel();tf.setText(counter.getCount()+ "");tfPanel.add(tf);add("North", tfPanel);Panel buttonPanel = new Panel();Button incButton = new Button("Increment");
incButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {counter.incCount();tf.setText(counter.getCount() + "");}
} );buttonPanel.add(incButton);
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 16/31
16
Behavioral Patterns - Observer MVC Example 2
Button decButton = new Button("Decrement");decButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
counter.decCount();tf.setText(counter.getCount()+ "");}
} );
buttonPanel.add(decButton);Button exitButton = new Button("Exit");exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {System.exit(0);
}} );buttonPanel.add(exitButton);add("South", buttonPanel);
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 17/31
17
Behavioral Patterns - Observer MVC Example 2
addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {
System.exit(0);}
} );}
public static void main(String[] argv) {Counter counter = new Counter(0);
CounterView cv1 = newCounterView("CounterView1", counter);cv1.setSize(300, 100);
cv1.setVisible(true);
CounterView cv2 = newCounterView("CounterView2", counter);cv2.setSize(300, 100);cv2.setVisible(true);}
}
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 18/31
18
Behavioral Patterns - Observer MVC Example 2
Next the model class:/*** Class Counter implements a simple counter model.*/public class Counter {
// The model.private int count;
public Counter(int count) { this.count = count; }public int getCount() { return count; }public void incCount() { count++; }public void decCount() { count--; }
}
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 19/31
19
Behavioral Patterns - Observer MVC Example 2
•Note that we instantiated one model and two views inthis example:
•But we have a problem! When the model changesstate, only one view updates!•We need the Observer Pattern here!
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 20/31
20
Behavioral Patterns - Observer MVC Example 3
This example shows the model and the view in separateclasses with the model being observable.
First the model class:
import java.util.Observable;/*** Class ObservableCounter implements a simpleobservable* counter model.*/public class ObservableCounter extends Observable {
// The model.private int count;public ObservableCounter(int count) { this.count = count;}
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 21/31
21
Behavioral Patterns - Observer MVC Example 3
public int getCount() { return count; }
public void incCount() {count++;setChanged();
notifyObservers();}
public void decCount() {count--;setChanged();notifyObservers();
}}
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 22/31
22
Behavioral Patterns - Observer MVC Example 3
Next the view class:
/*** Class ObservableCounterView demonstrates having the model* and view in the separate classes. This class is just the* view.*/public class ObservableCounterView extends Frame {
// The view.private TextField tf = new TextField(10);
// A reference to our associated model.private ObservableCounter counter;
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 23/31
23
Behavioral Patterns - Observer MVC Example 3
public ObservableCounterView(String title,ObservableCounter c) {
super(title);counter = c;
// Add an anonymous observer to the ObservableCounter.
counter.addObserver(new Observer() {public void update(Observable src, Object obj) {
if (src == counter) {tf.setText(((ObservableCounter)src).getCount() + "");}
}
} );
// Same GUI code as Example 2 not shown...
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 24/31
24
Behavioral Patterns - Observer MVC Example 3
public static void main(String[] argv) {ObservableCounter counter = new ObservableCounter(0);
ObservableCounterView cv1 = newObservableCounterView("ObservableCounterView1", counter);cv1.setSize(300, 100);
cv1.setVisible(true);
ObservableCounterView cv2 = newObservableCounterView("ObservableCounterView2", counter);cv2.setSize(300, 100);cv2.setVisible(true);}
}
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 25/31
25
Behavioral Patterns - Observer MVC Example 3
Looking good now!
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 26/31
26
Appendix: More on the Observer Pattern
Decouples a subject and its observers
Widely used in Smalltalk to separate application objects from interface objects
Known in the Smalltalk world as Model-View-Controller (MVC)
Rationale:
the interface is very l ikely to ch ange whi le the under ly ing b usiness o bjects remain
stable
Defines a subject (the Observable) that is observed
Allows multiple observers to monitor state changes in the subject withoutthe subject having explicit knowledge about the existence of theobservers
Subject
Observer
Observer
Observer
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 27/31
27
More on the Observer Pattern The Model-View-Controller (MVC)
Developed at Xerox Parc to provide foundation classes for Smalltalk-80
The Model, View and Controller classes have more than a 10 year history
Fundamental Principle
separate the underlying application MODEL (business objects) from the
INTERFACE (presentation objects)
Business Objects(the Model in MVC)
Expert Interface
Novice Interface
Rationale for MVC: Design for change and reuse
MVC and Observer Pattern In Smalltalk, objects may have dependents
When an object announces “I have changed”, its dependents are notified
It is the responsibility of the dependents to take action or ignore the notification
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 28/31
28
More on the Observer Pattern
java.util.Observable
Observable/subject objects (the Model in Model-View) can announce
that they have changed
Methods:
– void setChanged()
– void clearChanged()
– boolean hasChanged()
HarrysetChanged() hasChanged()
True/false
WHAT IF Observers query a Subject periodically?
Subject Observer query
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 29/31
29
More on the Observer Pattern Implementing & Checking an Observable
import java.util.*;import java.io.*;
public class Harry extends Observable {private boolean maritalStatus = false;
public Harry (boolean isMarried) {maritalStatus = isMarried;
}
public void updateMaritalStatus (boolean change) {maritalStatus = change;
// set flag for anyone interested to checkthis.setChanged();
}
Implementing an Observable
public static void main (String args [ ] ) {
Harry harry = new Harry (false);
harry.updateMaritalStatus (true);
if (harry.hasChanged() )
System.out.println ("Time to call harry");
}
Checking an Observable
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 30/31
31
Appendix: More on the Observer Pattern
Implementing the Observer Pattern
Harry Observer1
Observer2
addObserver (this)
addObserver (observer2)
Step 1: Observers register with Observable
update(Observable o, Object arg)HarrynotifyObservers(Object arg)
Observer1
Observable (Harry) may also send himself a notifyObservers() msg - no params
Step 2. Observable notifies Observers
7/27/2019 08_BehavioralPatterns.ppt
http://slidepdf.com/reader/full/08behavioralpatternsppt 31/31
32
Appendix: More on the Observer Pattern java.util.Observable
The superclass of all „observable‟ objects to be used in the Model
View design pattern
Methods are provided to: void addObserver(anObserver)
int countObservers() void deleteObserver (anObserver)
void deleteObservers ()
Interface
Defines the update() method that allows an object to „observe‟
subclasses of Observable
Objects that implement the interface may be passed as parameters in:
addObserver(Observer o)