- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Observer Pattern
Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.
Implementation
Observer pattern uses three actor classes. Subject, Observer and Cpent. Subject is an object having methods to attach and detach observers to a cpent object. We have created an abstract class Observer and a concrete class Subject that is extending class Observer.
ObserverPatternDemo, our demo class, will use Subject and concrete class object to show observer pattern in action.
Step 1
Create Subject class.
Subject.java
import java.util.ArrayList; import java.util.List; pubpc class Subject { private List<Observer> observers = new ArrayList<Observer>(); private int state; pubpc int getState() { return state; } pubpc void setState(int state) { this.state = state; notifyAllObservers(); } pubpc void attach(Observer observer){ observers.add(observer); } pubpc void notifyAllObservers(){ for (Observer observer : observers) { observer.update(); } } }
Step 2
Create Observer class.
Observer.java
pubpc abstract class Observer { protected Subject subject; pubpc abstract void update(); }
Step 3
Create concrete observer classes
BinaryObserver.java
pubpc class BinaryObserver extends Observer{ pubpc BinaryObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override pubpc void update() { System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) ); } }
OctalObserver.java
pubpc class OctalObserver extends Observer{ pubpc OctalObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override pubpc void update() { System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) ); } }
HexaObserver.java
pubpc class HexaObserver extends Observer{ pubpc HexaObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override pubpc void update() { System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() ); } }
Step 4
Use Subject and concrete observer objects.
ObserverPatternDemo.java
pubpc class ObserverPatternDemo { pubpc static void main(String[] args) { Subject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); System.out.println("First state change: 15"); subject.setState(15); System.out.println("Second state change: 10"); subject.setState(10); } }
Step 5
Verify the output.
First state change: 15 Hex String: F Octal String: 17 Binary String: 1111 Second state change: 10 Hex String: A Octal String: 12 Binary String: 1010Advertisements