原型模式

  • 原型模式Prototype Pattern用原型实例指向创建对象的类,使用于创建新的对象的类的共享原型的属性与方法
  • 简言之就是通过克隆来创建一个一模一样的对象。
  • 应用场景
    • 创建成本比较大的场景
    • 需要动态的获取当前的对象运行时状态的场景

js实现

  • 对于ES5,提供Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__
  • 对于ES6,提供Object.setPrototypeOf()方法设置一个指定的对象的原型(即,内部[[Prototype]]属性)到另一个对象或 null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Dog() {
this.name = "lili";
this.birthYear = 2015;
this.sex = "男";
this.presentYear = 2018;

this.getDiscription = () => {
return `狗狗叫${this.name},性别${this.sex},${this.presentYear}${this.presentYear - this.birthYear}岁了`
}
}

const dog = new Dog();
console.log(dog.getDiscription());// 狗狗叫lili,性别男,2018年3岁了
dog.presentYear = 2020;// 修改了当前年份
const dog1 = Object.create(dog);// 通过ES5语法进行创建
//const dog1 = {}
//Object.setPrototypeOf(dog1, dog);//通过ES6语法进行创建
console.log(dog.getDiscription());// 狗狗叫lili,性别男,2020年5岁了

ts实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
interface Prototype {
clone():Prototype;
}

class Dog implements Prototype {
public name: string;
public birthYear: number;
public sex: string;
public presentYear: number;
constructor() {
this.name = "lili";
this.birthYear = 2015;
this.sex = "男";
this.presentYear = 2018;
}

public getDiscription(): string {
return `狗狗叫${this.name},性别${this.sex},${this.presentYear}${this.presentYear - this.birthYear}岁了`
}

// 实现复制
public clone(): Prototype {
return Object.create(this);
}
}

// 使用
const dog = new Dog();
console.log(dog.getDiscription());
dog.presentYear = 2020;
const dog1 = Object.create(dog);
console.log(dog1.getDiscription());

参考链接