Here is a concise front-end knowledge system waiting for you to check, have a look, there will be a surprise oh, if you feel good, begged star ha ~
Brief introduction of factory Model
The factory model, as the name suggests, is about creating objects.
The factory model resembles a real factory production line and can produce a large number of similar goods.
Factory mode can be divided into simple factory mode and complex factory mode. The details are described below.
Simple Factory model
Let’s start with an example ~~~
function CarFactory (brand, price) {
var car = new Object();
car.brand = brand;
car.price = price;
car.getPrice = function () {
return this.price;
}
return car;
}
var car1 = CarFactory("A brand", 10000);
var car2 = CarFactory("B" brand, 20000); console.log(JSON.stringify(car1)); / / {"brand":"A brand"."price":10000} console.log(JSON.stringify(car2)); / / {"brand":"B" brand."price":20000}
console.log(typeof car1); // object
console.log(typeof car2); // object
console.log(car1 instanceof Object); // true
Copy the code
The CarFactory function takes two arguments, brand, price, and returns an object. If this function is called multiple times, it will return a new object each time, just like a factory line.
The advantage of the simple factory pattern is that it can solve many similar problems and reduce the amount of redundant code.
Of course, the disadvantage of the simple factory pattern is also prominent: the inability to recognize object types (typeof car1 === ‘object’)
In order to solve the problem that simple factory patterns cannot recognize object types, complex factory patterns are introduced
Complex factory model
Let’s take another example
function ComplexCarFactory(brand, price) {
this.brand = brand;
this.price = price;
}
ComplexCarFactory.prototype = {
constructor: ComplexCarFactory,
sellCar: function(){
var speed = this.getSpeed(this.brand);
console.log(this.brand + 'Car price:' + this.price + 'RMB yuan, speed limit' + speed + 'Kilometers per hour');
},
getSpeed : function(brand){
throw new Error("The parent class is an abstract class that can't be called directly, so you need a subclass to override this method."); }}; var CarChild =function(brand, price) { this.brand = brand; this.price = price; Complexcarfactory. call(this, brand, price); }; / / subclass inherits parent CarChild prototype method. The prototype = Object. The create (ComplexCarFactory. Prototype); / / CarChild subclasses override superclass method CarChild. Prototype. GetSpeed =function(brand){
var speed = null;
if(brand === 'brand C') {return 100;
}
return 50;
}
var car3 = new CarChild("C" brand, 3000);
console.log(car3); // CarChild {brand: "C" brand, price: 3000} console.log(car3.sellCar()); // The price of brand C is 3000 RMB with a speed limit of 50 km/hCopy the code
Code like this:
ComplexCarFactory is a parent class, CarChild is a subclass, and CarChild descends from ComplexCarFactory.
ComplexCarFactory does not instantiate objects. It only handles general problems in creation. ComplexCarFactory is like an abstract class in Java that must be overridden by subclasses. Otherwise, an exception is thrown when the sellCall method of ComplexCarFactory is called.
CarChild inherits from ComplexCarFactory and overrides the methods of its parent class. CarChild objects are independent of each other, and the specific business logic is written in subclasses.
conclusion
The most important advantage of the factory pattern is that some of the same methods can be implemented in the parent class, and the specific business logic to be implemented can be placed in the child class, and the child class can override the methods of the parent class to implement its own business logic.
The factory pattern reduces coupling between objects, with a parent class focused on implementing repetitive functionality and a subclass focused on concrete business logic, thus reducing redundant code.