简体   繁体   English

观察者模式是单向还是双向数据流

[英]Is observer pattern unidirectional or bidirectional data flow

I intend to use observer pattern to update views automatically once value of model has been changed. 一旦更改模型值,我打算使用观察者模式自动更新视图。 It's obvious that a model is observable (subject) and DOM elements are observers that are staring at model for any changes so that they can update themselves. 显而易见,模型是可观察的(主题),而DOM元素是观察者,他们盯着模型进行任何更改,以便他们可以自我更新。

But do you think it is still unidirectional data flow if I have a button which is observing model value and on click modify model value 但是,如果我有一个观察模型值并单击修改模型值的按钮,您认为它仍然是单向数据流吗?

The observer pattern is unidirectional , but your use case is bidirectional , while it's extending the observer pattern implementation. 观察者模式是单向的 ,但是您的用例是双向的 ,同时它扩展了观察者模式的实现。


The observer pattern defines, that the subject object will notify the observer if it's updated and the observer will implement an notify() function accordingly. 观察者模式定义,主题对象将在其更新时通知观察者,并且观察者将相应地实现notify()函数。

What you describe isn't part of that pattern. 您描述的内容不属于该模式。 You can implement it and if data flows it would be bidirectional. 您可以实现它,并且如果数据流将是双向的。 But it is not part of the pattern definition. 但这不是模式定义的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM