Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a factory in Angular

I was reading an article of Max NgWizard K, about how Angular updates the DOM. I came across the following:

For each component that is used in the application Angular compiler generates a factory. When Angular creates a component from a factory Angular uses this factory to instantiate View Definition which in turn is used to create component View. Under the hood Angular represents an application as a tree of views.

In another article from Max NgWizard K I found the definition of a factory:

Factories describe the structure of a component view and are used when instantiating the component.

I'm not really sure what is meant with this.

Questions:

  1. What exactly are factories in Angular(2+)?
  2. Are there scenarios that a developer benefits form knowing how they work?
like image 579
Willem van der Veen Avatar asked Mar 05 '18 16:03

Willem van der Veen


People also ask

What is a factory component?

A factory component is a composite activity. Structurally, it contains a set of viewpoints and a production plan. Behaviorally, it delegates its activities to other factory components or tasks.

Is AngularJS a factory Singleton?

Yes mate, factories and a services are both Singletons.

What are providers in AngularJS?

A provider is an object with a $get() method. The injector calls the $get method to create a new instance of a service. The Provider can have additional methods which would allow for configuration of the provider. AngularJS uses $provide to register new providers.

What is factory method in AngularJS Mcq?

Factory method in Angularjs is a simple function that allows us to add some logic to create an object and return it. The factory is used to create reusable code.

What is factory and service in AngularJS?

What is a Factory in AngularJS? The factory is a function in AngularJS that is used to return the values whenever the controller needs it. Once the value is created it can be reused by all the services and controllers. What is Service in AngularJS? In AngularJS, services are JavaScript functions that perform specific tasks.

What is a factory in C++?

In general factory is a creational design pattern. It is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class from some method call.

What is a 'factory' in Java?

'A factory' in this case is an instance of ComponentFactory, a class that has create method that implements Factory method pattern. When componentFactory.create is called (either directly or via ComponentFactoryResolver - which is essential for dynamic components, as linked article explains), new component instance is created.

What is the difference between service () and factory () function in JavaScript?

When we call service () function, factory () is the one that is actually called by passing a function that instantiates an object by the constructor. In simple words, we can say that service calls a predefined factory.


1 Answers

What exactly are factories in Angular(2+)?

Factory is one of the design patterns mentioned by Gang of Four (Basically they wrote a book on the design patterns they discovered).

Design Patterns help programmers solve common development tasks in a specific way.

And in this case, the Factory pattern helps in instantiation and creation of Objects.

It is also known as the Virtual Constructor.


Think of it, like this:

Say you are making a 2D shooter game, and you have to shoot bullets out of barrels.

Instead of instantiating bullets like new Bullet(), every time trigger is pulled, you can use a factory to create bullets, i.e. WeaponsFactory.createInstance(BulletTypes.AK47_BULLET).

It becomes highly scalable, since all you have to do is change the enum and the factory will make it for you.

You won't have to manually instantiate it.


That is what angular does, it automatically creates factory of all the components. Which makes its job easier.

Are there scenarios that a developer benefits form knowing how they work?

You don't have to know the inner workings of a Factory to use Angular, but it's useful for creating components dynamically!

e.g. A lot of *ngIf, or *ngSwitchCase can be replaced by a simple dynamic generation of components

Components can be created dynamically like this:

createComponent(type) {
  this.container.clear();
  const factory: ComponentFactory = this.resolver.resolveComponentFactory(AlertComponent);
  this.componentRef: ComponentRef = this.container.createComponent(factory);
}

Reference for understanding the above code: Dynamically Creating Components

like image 107
Abhijit Kar ツ Avatar answered Oct 21 '22 16:10

Abhijit Kar ツ