JSFiddle - React, Tailwind, and code Playground
by secretgspot
HTML
<h1>Factory pattern</h1>
When To Use This Pattern
The Factory pattern can be especially useful when applied to the falling situations:
When your object's setup requires a high level of complexity
When you need to generate different instances depending on the environment
When you're working with many small objects that share the same properties
When Not To Use This Pattern
It's generally a good practice to not use the factory pattern in every situation as it can easily add an unnecessarily additional aspect of complexity to your code. It can also make some tests more difficult to run.
JavaScript
var Car = (function() {
var Car = function (model, year, miles){
this.model = model;
this.year = year;
this.miles = miles;
};
return function (model, year, miles) {
return new Car(model, year, miles);
};
})();
var civic = new Car("Honda Civic", 2009, 20000);
var mondeo = new Car("Ford Mondeo", 2010, 5000);