js constructor pattern and factory pattern

1. Constructor mode
  1. Constructor

    When the memory of the new object is allocated, a special function used to initialize the object

  2. What is the constructor pattern

    The constructor mode is to create an object from the perspective of the constructor. By comparing the creation of objects with different constructors, the best performance is selected to create the object

  3. The way to create objects before es5

    // 1.字面量
    var obj = {
          
          
        uname: 'zt',
        age: 19
    }
    // 2.Object对象创建空对象 不方便对象属性赋值
    var obj2 = new Object();
    // 3.Object.create()方式常见可继承于某个对象的新对象,不直观
    var obj3 = Object.create();
    // 3.构造函数 不方便继承操作,每次新增对象让其指向同一个对象函数
    function Fn() {
          
          
        this.uname = 'zt',
        this.age = 19
    }
    var obj4 = new Fn();// 通过new 方式 将函数Fn中的属性uname等作为构造对象obj4的属性
    
  4. After es5

    Prototype allows child objects to share the properties of the parent object

2. Factory model
  1. What is the factory model

    It is also from the perspective of creating objects, but different from the constructor mode that explicitly requires the use of constructors to achieve, and this mode provides a public interface for creating objects, and the way to create objects is encapsulated in a factory, only calling the factory interface You can create different types of objects without knowing the specific implementation

  2. Case (using the factory pattern to create objects)

    // Types.js - Constructors used behind the scenes
    
    // A constructor for defining new cars
    function Car( options ) {
          
          
    
      // some defaults
      this.doors = options.doors || 4;
      this.state = options.state || "brand new";
      this.color = options.color || "silver";
    
    }
    
    // A constructor for defining new trucks
    function Truck( options){
          
          
    
      this.state = options.state || "used";
      this.wheelSize = options.wheelSize || "large";
      this.color = options.color || "blue";
    }
    
    // FactoryExample.js
    
    // Define a skeleton vehicle factory
    function VehicleFactory() {
          
          }
    
    // Define the prototypes and utilities for this factory
    
    // Our default vehicleClass is Car
    VehicleFactory.prototype.vehicleClass = Car;
    
    // Our Factory method for creating new Vehicle instances
    VehicleFactory.prototype.createVehicle = function ( options ) {
          
          
    
      if( options.vehicleType === "car" ){
          
          
        this.vehicleClass = Car;
      }else{
          
          
        this.vehicleClass = Truck;
      }
    
      return new this.vehicleClass( options );
    
    };
    
    // Create an instance of our factory that makes cars
    var carFactory = new VehicleFactory();
    var car = carFactory.createVehicle( {
          
          
                vehicleType: "car",
                color: "yellow",
                doors: 6 } );
    
    // Test to confirm our car was created using the vehicleClass/prototype Car
    
    // Outputs: true
    console.log( car instanceof Car );
    
    // Outputs: Car object of color "yellow", doors: 6 in a "brand new" state
    console.log( car );
    
  3. scenes to be used

    1. When creating objects is more complicated
    2. When you need to generate instances of different object types
    3. When some data needs to be shared between different object types
  4. Abstract factory

3. Abstract factory pattern
  1. A group of independent factories are packaged with a common goal.
  2. Use scenario: It is suitable for situations where the method of creating or generating objects must be independent from, or need to work with multiple types of objects.

Guess you like

Origin blog.csdn.net/chen__cheng/article/details/114477083