This is the 20th day of my participation in the August Challenge. Check out the August Challenge for details

preface

In real development, code that does not change is virtually nonexistent. The only thing we can do is to minimize the impact of this change — to separate the change from the constant, to make sure that the changed parts are flexible and the unchanged parts are stable.

This process is called “encapsulating change”; Such code is what we call “robust” code, it can withstand changes. Design patterns, which are just routines, help us write code like this. Factory mode, what it does is it takes away the process of creating objects, and today we’re going to talk about factory mode.

Simple Factory model

Factory patterns map factories in our daily lives. What are factories used for? It’s used to produce products, and when it comes to code, it’s a function that helps us generate different instances.

So when you talk about functions that generate different instances, that’s how constructors work.

Let’s take a look at this code to see if it’s familiar:

function createPerson(name, age, sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

const person = new createPerson('Dream Chasers'.18.'male');
console.log(person);
Copy the code

You can also change the new method to:

function createPerson(name, age, sex) {
  let obj = {};
  obj.name = name;
  obj.age = age;
  obj.sex = sex;
  return obj;
}

const person = createPerson('Dream Chasers'.18.'male');
console.log(person);
Copy the code

Complex factory model

Complex factory pattern: When we have enough properties and methods, we want to provide a common class, that is, a parent class, so that subclasses can inherit the properties and methods of the parent class for abstraction purposes.

That is, we now want to have a parent class that inherits properties and methods from the parent class. Each subclass can implement its own instance methods.

So the superclass solves the general problem, and the subclass solves the specific problem.

  1. Parent class: is an abstract class and cannot be directly instantiated
  2. A subclass implements its own instance methods

That might be a little hard to understand. Let’s take an example:

Now there would be a factory, a bicycle shop, that could have a name, and then there would be a way to get the name of the shop.

The bike shop, which is kind of a superclass, is an abstract class, what is it for? It’s used to sell bikes. The bike shop is not responsible for producing bikes.

function BicycleShop(name) {
  this.name = name;
  this.method = function () {
    return this.name;
  }
}

BicycleShop.prototype = {
  constructor: BicycleShop,
  sellBicycle: function () {
    var bicycle = this.CreateBicycle();

    bicycle.a();
    bicycle.b();

    return bicycle;
  },
  CreateBicycle: function () {
    throw new Error('Parent class can't be instantiated directly, you need a subclass to instantiate it.'); }}Copy the code

We talked about subclasses inheriting properties and methods from their parent classes. Before ES6, how did you implement inheritance? Use the following method: Sub is a subclass and Sup is a parent class

function extend(Sub, Sup) {
  var F = function() {};
  F.prototype = Sup.prototype;
  Sub.prototype = new F();
  Sub.prototype.constructor = Sub;
}
Copy the code

To implement subclasses, you need to use the extend method to implement the subclass to inherit the parent class

How does a subclass get the properties and methods of its parent class? With the call method, we borrow the constructor of the parent class.

function BicycleChild(name) {
  this.name = name;
  BicycleShop.call(this, name);
}

extend(BicycleChild, BicycleShop);

BicycleChild.prototype.createBicycle = function () {
  var a = function () {
    console.log('Perform task A');
  };
  var b = function () {
    console.log('Perform task B');
  };

  return {
    a,
    b
  }
}

var bicycleChild = new BicycleChild('the phoenix');
console.log(bicycleChild);
console.log(bicycleChild.createBicycle());
console.log(bicycleChild.sellBicycle());
Copy the code

Output result:

The complex factory pattern enables the implementation of abstract methods defined in the parent class, which can be abstracted out and subclasses can implement their own business logic.

Technically speaking, this is to decouple code from code. When there are more properties or methods in the parent class or factory mode, we need to decouple the code, decouple the object, and prevent code duplication.

Complex code, we can just throw it into the parent class, and then the subclass inherits all the properties and methods of the parent class, and the subclass just needs to do its own business logic.

Wrote last

Design patterns, which are just routines, help us write code like this. Factory mode, all it does is detaches the process of creating objects.

If there are any mistakes in this article, please correct them in the comments section. If this article helped you or you liked it, please like it and follow it.