Observer pattern
Observer pattern에 대해 설명하는 페이지입니다.
Environment
- Programming Language: Java
Index
Introduction
- a.k.a
- Publish/Subscibe model
- Purpose
- Lets one or more objects be notified of state changes in other objects within the system
- Use When
- Loose coupling is needed for communications
- State changes in one or more objects should trigger behavior in other objects
- Broadcasting capabilities are required.
- An understanding exists that objects will be blind to the expense of notification
- Definition
- It defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
How to Use
- Observable (Publisher) side
- To send notifications
- Call SetChanged()
- Call either notifyObservers() or notifyObservers(Object arg)
import java.util.Observable; import java.util.Observer; public class MyData extends Observable { private int number; public MyData() {} public void measurementsChanged() { setChanged(); notifyObservers(); } public void setMeasurements(int number) { this.number = number; measurementsChanged(); } public int getNumber() { return number; } }
- To send notifications
- Observer (Subsciber) side
- To become an observer
- Implement java.util.Observer interface
- To subscribe
- Call addObserver() on any Observable object
- To unsubscribe
- Call deleteObserver()
- To get updated
- Implement update(Observable o, Object arg)
import java.util.Observable; import java.util.Observer; public class MyObserver implements Observer { Observable observable; private int number; public MyObserver(Observable observable) { this.observable = observable; observable.addObserver(this); } public void update(Observable obs, Object arg) { if (obs instanceof MyData) { MyData myData = (MyData) obs; this.number = myData.getNumber(); // Something to execute } } }
- To become an observer