Create a design mode (on)

Singleton:

  1, define: singleton ensures that only a certain class of an instance , and to instantiate and the system provides this example , the class is called singleton

  2, the implementation process points:

    (1) singleton class constructor is private

    (2) itself provides a static private member variables

    (3) provides a public static factory method

  3 , the advantages:

    (1) provides the only example of controlled access

    (2) Since there is only one object in the system memory, it is possible to save system resources

    (3) allowing a variable number of instances. We can expand, using the singleton pattern based on one embodiment of a control method similar object instance to obtain a specified number of

  4. Cons:

    (1) No single embodiment mode abstraction layer, so a single type of embodiment extended with great difficulty

    (2) duty singleton too heavy, to a certain extent contrary to the "single responsibility principle"

    (3) a single case of abuse will bring some negative issues ; the use of language such as automatic garbage collection mechanism, instantiated object if the long-term is not used,

      It will be cleared, next time again you need to re-instantiated using this class, which led to the original state no

  5, for the environment:

    (1) The system requires only one instance of the object

    Single instance (2) class is called customer is only allowed to use a common access point , in addition to the common access point, other means can not access the instance

Singleton1 {class 
  Private static instance: Singleton1 = new new Singleton1 ();   // static member variables private 
  Private constructor () {}   // private constructor, which prevent external call generation Other examples 
  // public static factory method 
  static getInstance (): {Singleton1
     return Singleton1.instance; 
  } 
} 

const singleton_one: Singleton1 = Singleton1.getInstance (); 
const singleton_two: Singleton1 = Singleton1.getInstance (); 
the console.log (singleton_one === singleton_two); // to true

 

Builder mode:

  1、定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

  2、模式结构:

    (1)抽象建造者 Builder  (2)具体建造者 ConcreteBuilder

    (3)指挥者 Director    (4)产品角色 Product

  3、优点:

    (1)客户端不必知道产品内部组成细节,将产品本身与产品的创建过程解耦

    (2)每一个具体建造者都相对独立,与其他具体建造者无关,用户使用不同的具体建造者即可得到不同的产品对象

    (3)可以更加精细地控制产品的创建过程

    (4)增加新的具体建造者无须修改原有类的代码,指挥者类对抽象建造者类编程,系统扩展方便,符合“开闭原则”

  4、缺点:

    (1)建造者模式所创建的产品一般具有较多的共同点,其组成部分相似,

       如果产品之间的差异性很大,则不适合使用建造者模式,因此其使用范围受到一定的限制

    (2)如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大

  5、适用场景:

    (1)需要生成的产品对象有复杂的内部结构,这些产品对象通常包含多个成员属性

    (2)需要生成的产品对象的属性相互依赖,需要指定其生成顺序

    (3)对象的创建过程独立于创建该对象的类

    (4)隔离复杂对象的创建和使用,并使得相同的创建过程可以创建不同的产品

// 产品角色
class Computer {
    private cpu: string;
    private mainBoard: string;
    private memory: string;
    private hardDisk: string;

    setCpu(cpu: string): void {
        this.cpu = cpu;
    }
    setMainBoard(mainBoard: string): void {
        this.mainBoard = mainBoard;
    }
    setMemory(memory: string): void {
        this.memory = memory;
    }
    setHardDisk(hardDisk: string):void {
        this.hardDisk = hardDisk;
    }

    getCpu(): string {
        return this.cpu;
    }
    getMainBoard(): string {
        return this.mainBoard;
    }
    getMemory(): string {
        return this.memory;
    }
    getHardDisk(): string {
        return this.hardDisk;
    }
}

// 抽象建造者
interface Builder {
    createCpu(cpu: string): void;
    createMainBoard(mainBoard: string): void;
    createMemory(memory: string): void;
    createHardDisk(hardDisk: string): void;

    createComputer(): Computer;
}

// 具体建造者
class ConcreteBuilder implements Builder {
    private computer: Computer = new Computer();

    createCpu(cpu: string): void {
        this.computer.setCpu(cpu);
    }
    createMainBoard(mainBoard: string): void {
        this.computer.setMainBoard(mainBoard);
    }
    createMemory(memory: string): void {
        this.computer.setMemory(memory);
    }
    createHardDisk(hardDisk: string): void {
        this.computer.setHardDisk(hardDisk);
    }

    createComputer(): Computer {
        return this.computer;
    }
}

// 指挥者
class Director {
    private builder: Builder;
    constructor(builder: Builder) {
        this.builder = builder;
    }
    createComputer(cpu: string, mainBoard: string, memory: string, hardDisk: string): Computer {
        this.builder.createCpu(cpu);
        this.builder.createMainBoard(mainBoard);
        this.builder.createMemory(memory);
        this.builder.createHardDisk(hardDisk);

        return this.builder.createComputer();
    }
}

let builder: Builder = new ConcreteBuilder();
let director: Director = new Director(builder);
let computer: Computer = director.createComputer("i7", "Lenovo", "8G", "1T");
console.log(computer.getCpu()); // i7
console.log(computer.getMainBoard());   // Lenovo
console.log(computer.getMemory());  // 8G
console.log(computer.getHardDisk());    // 1T

 

原型模式:

  1、定义:用原型实例指定创建对象的种类,并且通过复用这些原型创建新的对象

  2、要点:

    (1)通过克隆来创建新的对象实例

    (2)为克隆出来的新的对象实例复制原型实例属性的值

  3、优点:

    (1)可以在运行时动态根据原型生成新的种类的对象

    (2)对客户端隐藏具体的实现类型

  4、缺点:

    每个原型的子类都必须实现 clone 的操作,尤其是包含引用类型的对象时,克隆会比较麻烦

  5、适用场景:

    (1)需要一个类的大量对象的时候,使用原型模式是最佳选择,

      因为原型模式是在内存中对这个对象进行拷贝,要比直接new这个对象性能要好很多

    (2)如果一个对象的初始化需要很多其他对象的数据准备或其他资源的繁琐计算,那么可以使用原型模式

    (3)当需要一个对象的大量公共信息,少量字段进行个性化设置的时候,

      也可以使用原型模式拷贝出现有对象的副本进行加工处理

// Prototype
interface Clonable<T> {
    clone(): T;
}
// ConcretePrototype
class Origin implements Clonable<Origin> {
    name: string;

    clone(): Origin {
        let target: Origin = new Origin();
        target.name = this.name;
        return target;
    }
}

let orign: Origin = new Origin();
orign.name = "Lemon";
let cloneObj: Origin = orign.clone();
console.log(cloneObj.name); // Lemon

 

Guess you like

Origin www.cnblogs.com/lemonyam/p/11617344.html