[JAVA] Observer Pattern

Multiple Observers monitor the state change of one object
The monitored class holds multiple Observer instances, and each Observer uses the methods of the Observer class to change the state.
Observer holds monitored class objects </ font>

Design Pattarm MENU

Check with the following class structure

class Explanation
Mediator.class Objects monitored by each Observer class
Store an instance of each Observer class in a List
Instance added to List()If so, notify all Observers of the size of the List
Hold a Mediator instance to be monitored
Implement the methods used by Mediator
ob1.class´Ż×ob2.class Implement Observer
user(Main.class) Operation check
  • User Includes the meaning that other developers use this pattern


class Mediator{
  List list = new ArrayList<Observer>();
  void add(Observer obsvr){
       notifyTo(list.size());  //NotifyTo when List is updated()Run
  void notifyTo(int size){
       Iterator it = list.iterator();
         Observer ob = (Observer) it.next();
         ob.update(size);      //Update of each Observer class stored in List()Notify using


abstract  class Observer{
     Mediator mediator = null;
     int      mediatorListSize = 0;
     Observer(Mediator med){this.mediator=med;}
     void update(int size){


class ob1 extends Observer{ob1(Mediator med){super(med);}}
class ob2 extends Observer{ob2(Mediator med){super(med);}}


public static void main(String[] args){
  Mediator md = new Mediator();
  md.add(new ob1(md));
  md.add(new ob2(md));

Recommended Posts

Observer Pattern
Design pattern ~ Observer ~
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
java callback pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java pattern memo