Hence, various variations of factory pattern are used as per requirements in each case.
The factory pattern pattern is an abstract way to create specific objects. Its a well known design pattern in software engineering.
Here, the functions are created to encapsulate the creation of objects with specific interfaces.
In the below demo, the functions createCar() accepts arguments to build an object with sufficient properties to represent a car.
Native constructors like object and array are automatically available to the user in execution enviornment during runtime.
To create custom constructors that define properties and methods to build your own type of objects use Constructor Pattern .
The difference between Factory Pattern and Constructor Pattern is as follows:
1. No object is created explicitly
2. The this object is assigned both the properties and methods.
3. There is no return statement to return an output.
The Prototype Pattern is used to create an object containing properties and methods which is available to instances of a particular reference type.
This is the prototype of the object to be created when the constructor is called.The advantage of this pattern is that all of its properties and methods are shared among instance objects.
Here the object information is not assigned to the constructor but its assigned directly to the prototype, follow the example below.
Custom type objects are created by combining the constructor and prototype pattern. Here, the constructor pattern defines the instance properties, while the prototype pattern defines method and shared properties
The advantage of this method is that each instance has its own copy of instance properties, while they all share references to methods, hence conserving precious memory space.
The instance properties are defined solely in the constructor, and the shared property constructor and the method printBrand() are defined on the property
The Dynamic Prototype hold all information within the constructor, at the same time maintaining the benefits of both a constructor and a prototype by inititalizing a prototype within a constructor, when needed.
You can determine if the prototype needed to be initialized by checking for the existence of a method that should be available.
The Parasitic Constructor pattern is used to create a constructor that wraps the creation and return of another object, but still look like a typical constructor.
This pattern should be used when other patterns fail as a fallback.
The Durable Constructor pattern is used to create objects that have no public properties and whose methods do not reference the this object.
This pattern should be used in secure enviornments(where use of this or new is not allowed) or to secure the data from the rest of applications.
The Durable pattern differs from Parasitic pattern in two ways:
1. The instance methods created on the object do not refer to this.
2. The Constructor is never called using the operator new.