Skip to content

Commit b0ecf01

Browse files
committed
Add additional information for the factory pattern
1 parent b54a217 commit b0ecf01

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ myModule.config(function ($provide) {
329329

330330
```
331331

332-
In the code above we use the `config` callback in order to define new "provider". Provider is an object, which has a method called `$get`. Since in JavaScript we don't have interfaces and the language is duck-typed there is a convention to name the factory method of the providers this way.
332+
In the code above we use the `config` callback in order to define new "provider". Provider is an object, which has a method called `$get`. Since in JavaScript we don't have interfaces and the language is duck-typed there is a convention to name the factory method of the providers that way.
333333

334334
Each service, filter, directive and controller has a provider (i.e. object which factory method, called `$get`), which is responsible for creating the component's instance.
335335

@@ -383,10 +383,16 @@ From the example above we can notice how the `$get` method is actually used:
383383
instanceInjector.invoke(provider.$get, provider, undefined, servicename)
384384
```
385385

386-
The snippet above calls the `invoke` method of `instanceInjector` with the factory method (i.e. `$get`) of given service, as first argument. Inside `invoke`'s body `annotate` is called with first argument the factory method. Annotate resolves all dependencies through the dependency injection mechanism of AngularJS. When all dependencies are resolved the factory method is being called: `fn.apply(self, args)`.
386+
The snippet above calls the `invoke` method of `instanceInjector` with the factory method (i.e. `$get`) of given service, as first argument. Inside `invoke`'s body `annotate` is called with first argument the factory method. Annotate resolves all dependencies through the dependency injection mechanism of AngularJS, which was considered above. When all dependencies are resolved the factory method is being called: `fn.apply(self, args)`.
387387

388388
If we think in terms of the UML diagram above we can call the provider a "ConcreteCreator" and the actual component, which is being created a "Product".
389389

390+
There are a few benefits of using the factory method pattern in this case, because of the indirection it creates. This way the framework can take care of the boilerplates during the instantiation of new components like:
391+
392+
- The most appropriate moment, when the component needs to be instantiated
393+
- Resolving all the dependencies required by the component
394+
- The number of instances the given component is allowed to have (for services and filters only a single one but multiple for the controllers)
395+
390396
#### Decorator
391397

392398
>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.

0 commit comments

Comments
 (0)