// 装饰器模式: 允许向一个现有的对象添加新的功能,同时又不改变其结构
class Phone {
// 原对象
call() {
console.log('打电话');
}
}
class Decorator {
// 新对象即新功能添加的装饰器
constructor (phone) {
this.phone = phone
}
call () {
this.antiFall();
this.phone.call();
}
antiFall () {
console.log('防止坠落');
}
}
class Client {
// 调用装饰器的对象
constructor () {
const phone = new Phone();
this.decorator = new Decorator(phone);
}
main () {
this.decorator.call();
}
}
const client = new Client();
client.main();
JavaScript的设计模式-装饰器模式
猜你喜欢
转载自blog.csdn.net/memedadexixaofeifei/article/details/103857466
今日推荐
周排行