Observer pattern

Observer pattern

 

  • behavioral pattern
     
  • one-to-many dependency

Subject

Observers

Subject

Observers

state = 'A'        

Subject

Observers

state = 'A'        

Subject

Observers

Subject

Observers

state = 'B'        

Subject

Observers

state = 'B'        

Subject

Observers

loosely coupled

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

subject.subscribe(this)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

subject.subscribe(this)

subject.subscribe(this)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

subject.unsubscribe(this)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

subject.unsubscribe(this)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

state = 'A'        

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

state = 'A'        

update()

Subject

Observers

.notify()

.unsubscribe(observer)

.subscribe(observer)

Subject

Observers

Observer pattern

By Enrique Oriol

Observer pattern

  • 42