设计模式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 updatedautomatically.
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 listeneris 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 Listof
Peopleobjects.
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"); } } }
相关文章推荐
- 设计模式笔记 18. Observer 观察者模式(行为型模式)
- 设计模式18:Observer 观察者模式(行为型模式)
- 观察者设计模式-Observable类和Observer接口
- 24种设计模式--观察者模式【Observer Pattern】
- .NET设计模式(19):观察者模式(Observer Pattern)
- 学习php设计模式 php实现观察者模式(Observer)
- Java开发中的23种设计模式详解----观察者模式(Observer)
- 设计模式——观察者模式(Observer Pattern)
- 观察者设计模式(Observable类Observer接口)的实现demo
- 设计模式----观察者模式Observer(C++)
- 设计模式之 观察者模式 the observer design pattern ---------《head first design pattern》
- C++设计模式-Observer观察者模式
- 步步为营 .NET 设计模式学习笔记 十二、Observer (观察者模式)
- 设计模式之Observer(观察者模式)
- Java设计模式之——观察者模式(Observer)
- 设计模式之观察者模式(Observer)
- Java设计模式——Observer(观察者)模式
- Java设计模式-----Observer观察者模式
- 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
- .NET实用设计模式:观察者模式(Observer)