You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.
There are two basic ways of communication between the scopes in an AngularJS application. The first one is calling methods of parent scope by a child scope. This is possible since the child scope inherits prototypically by its parent, as mentioned above (see [Scope](#scope)). This allows communication in a single direction - child to parent. Some times it is necessary to call method of given child scope or notify it about a triggered event in the context of the parent scope. AngularJS provides built-in observer pattern, which allows this. Another possible use case, of the observer pattern, is when multiple scopes are interested in given event but the scope, in which context the event is triggered, is not aware of them. This allows decoupling between the different scopes, non of the scopes should be aware of the rest of the scopes.
Each AngularJS scope has public methods called `$on`, `$emit` and `$broadcast`. The method `$on` accepts topic as first argument and callback as second. We can think of the callback as an observer - an object, which implements the `Observer` interface (in JavaScript the functions are first-class, so we can provide only implementation of the `notify` method):
1172
+
-->
1162
1173
1163
1174
```JavaScript
1164
1175
functionExampleCtrl($scope) {
@@ -1168,27 +1179,45 @@ function ExampleCtrl($scope) {
In this way the current scope "subscribes" to events of type `event-name`. When `event-name` is triggered in any parent or child scope of the given one, `handler` would be called.
The scope in the example above, triggers the event `event-name` to all scopes upwards. This means that each of the parent scopes of the given one, which are subscribed to the event `event-name`, would be notified and their handler callback will be invoked.
Analogical is the case when the method `$broadcast` is called. The only difference is that the event would be transmitted downwards - to all children scopes.
1185
1207
Each scope can subscribe to any event with multiple callbacks (i.e. it can associate multiple observers to given event).
1208
+
-->
1186
1209
1210
+
在 JavaScript 社区中,这种模式又被称为发布/订阅模式。
1211
+
<!--
1187
1212
In the JavaScript community this pattern is better known as publish/subscribe.
For a best practice example see [Observer Pattern as an External Service](#observer-pattern-as-an-external-service)
1218
+
-->
1190
1219
1191
-
#### Chain of Responsibilities
1220
+
#### <aname='chain-of-responsibilities'>责任链模式 (Chain of Responsibilities)</a>
1192
1221
1193
1222
>The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.
1194
1223
@@ -1517,7 +1546,7 @@ function ObserverExample(ObserverService, $timeout, $scope) {
5. [企业应用架构模式 (P of EAA)](http://martinfowler.com/books/eaa.html)
1522
1551
6. [Using Dependancy Injection to Avoid Singletons](http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html)
1523
1552
7. [Why would one use the Publish/Subscribe pattern (in JS/jQuery)?](https://stackoverflow.com/questions/13512949/why-would-one-use-the-publish-subscribe-pattern-in-js-jquery)
0 commit comments