티스토리 뷰

Java

The Observer Pattern, in the Real World

techbard 2017.04.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.


댓글
댓글쓰기 폼
공지사항
Total
395,135
Today
3
Yesterday
41
«   2018/11   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
글 보관함