factory pattern
Factory method pattern - Wikipedia
factory pattern - javascript
> JavaScript Design Patterns: Factory ] by Rob Dodson
Dodson formally defines the Factory pattern the 3 different "versions" of implementing Factory designs .
Factories encapsulate and separate object creation from the rest of your code. In situations where the creation of an object can be complex or subject to change a factory can act as a nice buffer to help keep things tidy. Without proper planning Factories can lead to class explosions; as a result the pattern can be both a blessing and a curse depending on how it's used.
Simple Factory - is an object which encapsulates the creation of another object, shielding that code from the rest of your application. It's common to parameterize simple factory methods to increase the number of products they're able to return. Typically the return value from a factory is known as the Product
Factory Method - Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. (aka Virtual Constructor ) Factory Method defines one method, createThing for instance, which is overriden by subclasses who decide what to return. The Factories and Products must conform to interfaces for clients to be able to use them. The main take away is that there is only one method, (createPizza, )that does anything. By subclassing and overriding this method we can offer aditional flexibility beyond what's possible with the Simple Factory.
Abstract Factory - Provide an interface for creating families of related or dependent objects without specifying their concrete classes. ( aka Kit )
Dodson goes on to provide code implementations of each of the various implementations of the Factory design in JavaScript. The includes implementing an Abstract Factory in the JS language which lacks the language feature of being able to define an interface
> http://www.dofactory.com/javascript/factory-method-design-pattern -
A simple example, create different types of employees using a Factory with a createEmployee(type) method
A Factory Method creates new objects as instructed by the client. One way to create objects in JavaScript is by invoking a constructor function with the new operator. There are situations however, where the client does not, or should not, know which one of several candidate objects to instantiate. The Factory Method allows the client to delegate object creation while still retaining control over which type to instantiate.
The key objective of the Factory Method is extensibility. Factory Methods are frequently used in applications that manage, maintain, or manipulate collections of objects that are different but at the same time have many characteristics (i.e. methods and properties) in common. An example would be a collection of documents with a mix of Xml documents, Pdf documents, and Rtf documents.
> The Factory Pattern in JS ES6 – SntsDev – Medium
....
ID: 5736
NAME: factory-pattern-overview
DESCRIPTION: ] factory pattern overview
AUTHOR: article.author/s
EDITOR: article.editor/s
PUBLISHER: article.publisher/s
STATUS: Write
PRIORITY: -5
OWNER ID: 75