您的位置:首页 > 其它

设计模式18 - Observer 观察者模式

2012-12-31 09:24 357 查看


1. Observer Pattern


1.1. Definition

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated
automatically.
The object which is being watched is called the subject.The objects which are watching the state changes are called observer. Alternatively observer are
also called listener.


1.2. Example

The observer pattern is very common in Java. For example you would define a listener for a button in a user interface. If the button is selected the listener
is notified and perform a certain action. Similar to text field could add a listener for keyevents.
But the observer pattern is not limited to single user interface components. For example you could have a part A in your application which displays the current temperature.
Another part B displays a green light is the temperature is above 20 degree celsius. To react to changes in the temperature part B registers itself as a listener to Part A.
If the temperature in part A is changed, an event is triggered. This event is send to all registered listeners, as for example part B. Part B receives the changed data and can adjust his display.


1.3. Code example

In the following example the observer is watching changes in a
List
of
People
objects.

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

public class MyModel {
private List<Person> persons = new ArrayList<Person>();
private PropertyChangeListener listener;

public class Person{
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
notifyListeners(this, "firstName", this.firstName, this.firstName = firstName);
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
notifyListeners(this, "lastName", this.lastName, this.lastName = lastName);
}
}// class Person

public MyModel() {
// Just for testing:
persons.add(new Person("Lars", "Vogel"));
persons.add(new Person("Jim", "Knopf"));
}
public List<Person> getPersons() {
return persons;
}
private void notifyListeners(Object object, String property, String oldValue, String newValue) {
listener.propertyChange(new PropertyChangeEvent(this, property, oldValue, newValue));
}
public void addChangeListener(PropertyChangeListener newListener) {
listener = newListener;
}
}


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class MyObserver implements PropertyChangeListener {
public MyObserver(MyModel model) {
model.addChangeListener(this);
}
public void propertyChange(PropertyChangeEvent event) {
System.out.println("Changed property: " + event.getPropertyName()
+ " old: " + event.getOldValue()
+ " new: " + event.getNewValue());
}
}


public class Main {
public static void main(String[] args) {
MyModel model = new MyModel();
MyObserver observer = new MyObserver(model);
// We change the last name of the person, observer will get notified
for (MyModel.Person person : model.getPersons()) {
person.setLastName(person.getLastName() + "1");
}
// We change the first name of the person, observer will get notified
for (MyModel.Person person : model.getPersons()) {
person.setFirstName(person.getFirstName() + "0");
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: