How does the Observer pattern reduce coupling?

耗尽温柔 提交于 2019-12-06 06:04:23

问题


I understand how the Observer pattern works, but why is it that the Observer pattern reduces coupling between UI and business logic components in software design?


回答1:


The Observer pattern reduces coupling among its participants because it introduces an abstract type, Observer, between the Subject and its Observers.

Imagine a Model (the Subject in the Gang of Four/Wikipedia description, and the home of business logic) and a View (an Observer). Without Observer, the Model would need to call a method on the View whenever it changed. The Model would know the concrete class of View and be coupled to it, and to whatever UI-specific framework the View was a part of.

With Observer, the Model only knows about the type (abstract class or interface) Observer, so it is not coupled to the concrete View.



来源:https://stackoverflow.com/questions/36868384/how-does-the-observer-pattern-reduce-coupling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!