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
*[Chain of Responsibilities](#chain-of-responsibilities)
35
-
*[Command](#command)
36
-
*[Page Controller](#page-controller)
37
-
*[Module Pattern](#module-pattern)
38
-
*[Data Mapper](#data-mapper)
22
+
*[AngularJS in Patterns](#angularjs-in-patterns)
39
23
*[AngularJS](#angularjs)
40
24
*[Services](#services-1)
41
25
*[Singleton](#singleton)
@@ -59,6 +43,17 @@
59
43
*[Module Pattern](#module-pattern)
60
44
*[Data Mapper](#data-mapper)
61
45
*[Angular 2](#angular-2)
46
+
*[Components](#components-1)
47
+
*[Composite](#composite-2)
48
+
*[Memento](#memento)
49
+
*[Interpreter](#interpreter-2)
50
+
*[Visitor](#visitor)
51
+
*[Strategy](#strategy)
52
+
*[Builder](#builder)
53
+
*[Adapter](#adapter)
54
+
*[Facade](#facade-2)
55
+
*[Object Pool](#object-pool)
56
+
62
57
*[References](#references)
63
58
64
59
<!--endtoc-->
@@ -276,21 +271,15 @@ function MyCtrl(Developer) {
276
271
}
277
272
```
278
273
279
-
## AngularJS Patterns
274
+
## AngularJS in Patterns
280
275
281
276
In the next a couple of sections, we are going to take a look how the traditional design and architectural patterns are composed in the AngularJS components.
282
277
283
278
In the last chapter we are going to take a look at some architectural patterns, which are frequently used in the development of Single-Page Applications with (but not limited to) AngularJS.
284
279
285
280
### Services
286
281
287
-
#### Singleton
288
-
289
-
>The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
290
-
291
-
In the UML diagram bellow is illustrated the singleton design pattern.
When given dependency is required by any component, AngularJS resolves it using the following algorithm:
296
285
@@ -338,9 +327,7 @@ For further discussion on this topic Misko Hevery's [article](http://googletesti
338
327
339
328
#### Factory Method
340
329
341
-
>The factory method pattern is a creational pattern, which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created. This is done by creating objects via a factory method, which is either specified in an interface (abstract class) and implemented in implementing classes (concrete classes); or implemented in a base class, which can be overridden when inherited in derived classes; rather than by a constructor.
@@ -428,9 +415,7 @@ There are a few benefits of using the factory method pattern in this case, becau
428
415
429
416
#### Decorator
430
417
431
-
>The decorator pattern (also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
AngularJS provides out-of-the-box way for extending and/or enhancing the functionality of already existing services. Using the method `decorator` of `$provide` you can create "wrapper" of any service you have previously defined or used by a third-party:
436
421
@@ -468,17 +453,7 @@ Using this pattern is especially useful when we need to modify the functionality
468
453
469
454
#### Facade
470
455
471
-
>A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:
472
-
473
-
>1. make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
474
-
475
-
>2. make the library more readable, for the same reason;
476
-
477
-
>3. reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
478
-
479
-
>4. wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).
There are a few facades in AngularJS. Each time you want to provide higher level API to given functionality you practically create a facade.
484
459
@@ -527,9 +502,7 @@ Even higher level of abstraction is being created by `$resource`, which is build
527
502
528
503
#### Proxy
529
504
530
-
>A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.
We can distinguish three different types of proxy:
535
508
@@ -565,9 +538,7 @@ Initially when the snippet above executes, the property `user` of the `$scope` o
565
538
566
539
#### Active Record
567
540
568
-
>The Active Record object is an object, which carries both data and behavior. Usually most of the data in these objects is persistent, responsibility of the Active Record object is to take care of the communication with the database in order to create, update, retrieve or delete the data. It may delegate this responsibility to lower level objects but calls to instance or static methods of the active record object cause the database communication.
AngularJS defines a service called `$resource`. In the current version of AngularJS (1.2+) it is being distributed in module outside of the AngularJS' core.
573
544
@@ -608,9 +579,7 @@ Since Martin Fowler states that
608
579
609
580
#### Intercepting Filters
610
581
611
-
>Create a chain of composable filters to implement common pre-processing and post-processing tasks during a Web page request.
In some cases you need to do some kind of pre and/or post processing of HTTP requests. In the case of the Intercepting Filters you pre/post process given HTTP request/response in order to include logging, security or any other concern, which is influenced by the request body or headers. Basically the Intercepting Filters pattern include a chain of filters, each of which process data in given order. The output of each filter is input of the next one.
>The composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
According to the Gang of Four, MVC is nothing more than combination of:
645
612
@@ -686,9 +653,7 @@ In the second, JavaScript, example we see that the `template` property of the di
686
653
687
654
### Interpreter
688
655
689
-
>In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence.
Behind its `$parse` service, AngularJS provides its own implementation of interpreter of a DSL (Domain Specific Language). The used DSL is simplified and modified version of JavaScript.
694
659
The main differences between the JavaScript expressions and AngularJS expressions that AngularJS expressions:
@@ -756,9 +721,7 @@ Few sample AngularJS expressions are:
756
721
757
722
#### Template View
758
723
759
-
> Renders information into HTML by embedding markers in an HTML page.
The dynamic page rendering is not that trivial thing. It is connected with a lot of string concatenations, manipulations and frustration. Far easier way to build your dynamic page is to write your markup and embed little expressions inside it, which are lately evaluated in given context and so the whole template is being compiled to its end format. In our case this format is going to be HTML (or even DOM). This is exactly what the template engines do - they take given DSL, evaluate it in the appropriate context and then turn it into its end format.
764
727
@@ -813,9 +776,7 @@ will produce the same result as the one above. The main difference here is that
813
776
814
777
#### Observer
815
778
816
-
>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.
821
782
@@ -849,9 +810,7 @@ In the JavaScript community this pattern is better known as publish/subscribe.
849
810
850
811
#### Chain of Responsibilities
851
812
852
-
>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.
853
-
854
-

813
+
<!-- include chain-of-responsibilities -->
855
814
856
815
As stated above the scopes in an AngularJS application form a hierarchy known as the scope chain. Some of the scopes are "isolated", which means that they don't inherit prototypically by their parent scope, but are connected to it via their `$parent` property.
857
816
@@ -886,9 +845,7 @@ The different handlers from the UML diagram above are the different scopes, inje
886
845
887
846
#### Command
888
847
889
-
>In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
According to [4](#references) the page controller:
939
894
@@ -983,34 +938,7 @@ The `ChildCtrl` is responsible for handling actions such as clicking the button
983
938
984
939
#### Module Pattern
985
940
986
-
This is actually not a design pattern from Gang of Four, neither one from P of EAA. This is a traditional JavaScript pattern, which main goal is to provide encapsulation and privacy.
987
-
988
-
Using the module pattern you can achieve privacy based on the JavaScript's functional lexical scope. Each module may have zero or more private members, which are hidden in the local scope of a function. This function returns an object, which exports the public API of the given module:
989
-
990
-
```javascript
991
-
var Page = (function () {
992
-
993
-
var title;
994
-
995
-
functionsetTitle(t) {
996
-
document.title= t;
997
-
title = t;
998
-
}
999
-
1000
-
functiongetTitle() {
1001
-
return title;
1002
-
}
1003
-
1004
-
return {
1005
-
setTitle: setTitle,
1006
-
getTitle: getTitle
1007
-
};
1008
-
}());
1009
-
```
1010
-
1011
-
In the example above we have IIFE (Immediately-Invoked Function Expression), which after being called returns an object, with two methods (`setTitle` and `getTitle`). The returned object is being assigned to the `Page` variable.
1012
-
1013
-
In this case the user of the `Page` object doesn't has direct access to the `title` variable, which is defined inside the local scope of the IIFE.
941
+
<!-- include module-pattern -->
1014
942
1015
943
The module pattern is very useful when defining services in AngularJS. Using this pattern we can simulate (and actually achieve) privacy:
1016
944
@@ -1037,9 +965,7 @@ Once we want to inject `foo` inside any other component we won't be able to use
1037
965
1038
966
### Data Mapper
1039
967
1040
-
>A Data Mapper is a Data Access Layer that performs bidirectional transfer of data between a persistent data store (often a relational database) and an in memory data representation (the domain layer). The goal of the pattern is to keep the in memory representation and the persistent data store independent of each other and the data mapper itself.
As the description above states, the data mapper is used for bidirectional transfer of data between a persistent data store and an in memory data representation. Usually our AngularJS application communicates with API server, which is written in any server-side language (Ruby, PHP, Java, JavaScript, etc.).
0 commit comments