问题描述
我了解Observer模式是如何工作的,但是为什么Observer模式减少了软件设计中UI和业务逻辑组件之间的耦合?
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?
推荐答案
观察者模式减少了参与者之间的耦合,因为它在主题及其观察者之间引入了抽象类型Observer.
The Observer pattern reduces coupling among its participants because it introduces an abstract type, Observer, between the Subject and its Observers.
想象一个模型(四人一组的主题/维基百科描述,以及业务逻辑的家园)和一个视图(一个观察者).如果没有Observer,则模型在更改时将需要在View上调用方法.该模型将知道View的具体类并将其与View所组成的任何特定于UI的框架耦合.
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.
在使用Observer的情况下,模型仅知道类型(抽象类或接口)的Observer,因此它不会与具体的View耦合.
With Observer, the Model only knows about the type (abstract class or interface) Observer, so it is not coupled to the concrete View.
这篇关于观察者模式如何减少耦合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!