Skip to content

Commit 8e1cb63

Browse files
committed
Externalize the patterns update content
1 parent ad7f5c2 commit 8e1cb63

File tree

2 files changed

+163
-103
lines changed

2 files changed

+163
-103
lines changed

README.md

Lines changed: 29 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,7 @@
1919
* [Services](#services)
2020
* [Dependency Injection](#dependency-injection)
2121
* [Zones](#zones)
22-
* [AngularJS Patterns](#angularjs-patterns)
23-
* [Catalog](#catalog)
24-
* [Singleton](#singleton)
25-
* [Factory Method](#factory-method)
26-
* [Decorator](#decorator)
27-
* [Facade](#facade)
28-
* [Proxy](#proxy)
29-
* [Active Record](#active-record)
30-
* [Intercepting Filters](#intercepting-filters)
31-
* [Composite](#composite)
32-
* [Template View](#template-view)
33-
* [Observer](#observer)
34-
* [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)
3923
* [AngularJS](#angularjs)
4024
* [Services](#services-1)
4125
* [Singleton](#singleton)
@@ -59,6 +43,17 @@
5943
* [Module Pattern](#module-pattern)
6044
* [Data Mapper](#data-mapper)
6145
* [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+
6257
* [References](#references)
6358

6459
<!--endtoc-->
@@ -276,21 +271,15 @@ function MyCtrl(Developer) {
276271
}
277272
```
278273

279-
## AngularJS Patterns
274+
## AngularJS in Patterns
280275

281276
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.
282277

283278
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.
284279

285280
### Services
286281

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.
292-
293-
![Singleton](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/singleton.svg "Fig. 1")
282+
<!-- include singleton -->
294283

295284
When given dependency is required by any component, AngularJS resolves it using the following algorithm:
296285

@@ -338,9 +327,7 @@ For further discussion on this topic Misko Hevery's [article](http://googletesti
338327

339328
#### Factory Method
340329

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.
342-
343-
![Factory Method](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/factory-method.svg "Fig. 2")
330+
<!-- include factory-method -->
344331

345332
Lets consider the following snippet:
346333

@@ -428,9 +415,7 @@ There are a few benefits of using the factory method pattern in this case, becau
428415

429416
#### Decorator
430417

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.
432-
433-
![Decorator](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/decorator.svg "Fig. 4")
418+
<!-- include decorator -->
434419

435420
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:
436421

@@ -468,17 +453,7 @@ Using this pattern is especially useful when we need to modify the functionality
468453

469454
#### Facade
470455

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).
480-
481-
![Facade](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/facade.svg "Fig. 11")
456+
<!-- include facade -->
482457

483458
There are a few facades in AngularJS. Each time you want to provide higher level API to given functionality you practically create a facade.
484459

@@ -527,9 +502,7 @@ Even higher level of abstraction is being created by `$resource`, which is build
527502

528503
#### Proxy
529504

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.
531-
532-
![Proxy](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/proxy.svg "Fig. 9")
505+
<!-- include proxy -->
533506

534507
We can distinguish three different types of proxy:
535508

@@ -565,9 +538,7 @@ Initially when the snippet above executes, the property `user` of the `$scope` o
565538

566539
#### Active Record
567540

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.
569-
570-
![Active Record](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/active-record.svg "Fig. 7")
541+
<!-- include active-record -->
571542

572543
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.
573544

@@ -608,9 +579,7 @@ Since Martin Fowler states that
608579

609580
#### Intercepting Filters
610581

611-
>Create a chain of composable filters to implement common pre-processing and post-processing tasks during a Web page request.
612-
613-
![Composite](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/intercepting-filters.svg "Fig. 3")
582+
<!-- include intercepting-filters -->
614583

615584
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.
616585

@@ -637,9 +606,7 @@ $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
637606

638607
#### Composite
639608

640-
>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.
641-
642-
![Composite](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/composite.svg "Fig. 3")
609+
<!-- include composite -->
643610

644611
According to the Gang of Four, MVC is nothing more than combination of:
645612

@@ -686,9 +653,7 @@ In the second, JavaScript, example we see that the `template` property of the di
686653

687654
### Interpreter
688655

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.
690-
691-
![Interpreter](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/interpreter.svg "Fig. 6")
656+
<!-- include interpreter -->
692657

693658
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.
694659
The main differences between the JavaScript expressions and AngularJS expressions that AngularJS expressions:
@@ -756,9 +721,7 @@ Few sample AngularJS expressions are:
756721

757722
#### Template View
758723

759-
> Renders information into HTML by embedding markers in an HTML page.
760-
761-
![Template View](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/template-view.svg "Fig. 8")
724+
<!-- include template-view -->
762725

763726
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.
764727

@@ -813,9 +776,7 @@ will produce the same result as the one above. The main difference here is that
813776

814777
#### Observer
815778

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.
817-
818-
![Observer](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/observer.svg "Fig. 7")
779+
<!-- pattern observer -->
819780

820781
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.
821782

@@ -849,9 +810,7 @@ In the JavaScript community this pattern is better known as publish/subscribe.
849810

850811
#### Chain of Responsibilities
851812

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-
![Chain of Responsibilities](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/chain-of-responsibilities.svg "Fig. 5")
813+
<!-- include chain-of-responsibilities -->
855814

856815
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.
857816

@@ -886,9 +845,7 @@ The different handlers from the UML diagram above are the different scopes, inje
886845

887846
#### Command
888847

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.
890-
891-
![Command](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/command.svg "Fig. 11")
848+
<!-- include command -->
892849

893850
Before continuing with the application of the command pattern lets describe how AngularJS implements data binding.
894851

@@ -931,9 +888,7 @@ We can think of the `watcher` object as a command. The expression of the command
931888
932889
#### Page Controller
933890
934-
>An object that handles a request for a specific page or action on a Web site. Martin Fowler
935-
936-
![Page Controller](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/page-controller.svg "Fig. 8")
891+
<!-- include page-controller -->
937892
938893
According to [4](#references) the page controller:
939894
@@ -983,34 +938,7 @@ The `ChildCtrl` is responsible for handling actions such as clicking the button
983938
984939
#### Module Pattern
985940
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-
function setTitle(t) {
996-
document.title = t;
997-
title = t;
998-
}
999-
1000-
function getTitle() {
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 -->
1014942
1015943
The module pattern is very useful when defining services in AngularJS. Using this pattern we can simulate (and actually achieve) privacy:
1016944
@@ -1037,9 +965,7 @@ Once we want to inject `foo` inside any other component we won't be able to use
1037965
1038966
### Data Mapper
1039967
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.
1041-
1042-
![Data Mapper](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/data-mapper.svg "Fig. 10")
968+
<!-- include data-mapper -->
1043969
1044970
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.).
1045971

0 commit comments

Comments
 (0)