상세 컨텐츠

본문 제목

The Observer Pattern, in the Real World

Java

by techbard 2017. 4. 28. 17:29

본문

반응형

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 updated automatically.




Being Loosely Coupled


- Subjects and observers are "loosely coupled"

They interact,

but have little knowledge of each other.


- Subject only knows

The observer implements a specific interface.

Does not need to know concrete class or anything else.


The Observer


- Subject relies on a list of observers.


- Observers can be:

+ Added at any time.

+ Removed at any time.

+ Or even replaced.


- Subject does not care, it keeps doing its job.


If we want to add new type of observers, modification of the subject is never needed.

- Observers just need to implement the Observer interface.


In fact, no changes to the subject or observers will affect the other.


> That's loose coupling.


Because object interdependencies are minimized, loose coupling allows us to build more flexible OO architectures.


Design Principle #4


Strive for loosely coupled designs between objects that interact.


반응형

관련글 더보기

댓글 영역