原型模式
什么是原型模式
类并不是必须的,对象未必需要从类中创建而来,一个对象是通过克隆另外一个对象所得到的。
原型模式不但是一种设计模式,也被称为一种编程泛型。
ECMAScript5 提供了 Object.create 方法,可以用来克隆对象。
但是 create 方法性能不如 var obj = {} 或者 var obj = new Object();
以上两种替代方式,内部都是克隆原型而得到对象。
注: Javascript 中的根对象是 Object,所有的对象都从根对象克隆而来。
函数式例子:
/**
* 使用原型继承方式实现一个模板方法模式。
* 通过设置钩子方法,可以实现不同的子类使用不同的父类约束。
*/
(function () {
var Beverage = function () {};
Beverage.prototype.boilWater = function () {
console.log('把水煮沸。');
};
Beverage.prototype.brew = function () {
throw new Error('子类必须重写冲泡方法');
};
Beverage.prototype.poutInCup = function () {
throw new Error('子类必须重写把饮料倒入杯子方法');
};
Beverage.prototype.addCondiments = function () {
throw new Error('子类必须重写加调料方法');
};
Beverage.prototype.customerAddCondiments = function () {
return true;
}
Beverage.prototype.init = function () {
this.boilWater();
this.brew();
this.poutInCup();
if (this.customerAddCondiments()) {
this.addCondiments();
}
}
var Coffee = function () {};
Coffee.prototype = new Beverage();
Coffee.prototype.brew = function () {
console.log('冲泡咖啡');
};
Coffee.prototype.poutInCup = function () {
console.log('把咖啡倒进杯子');
};
Coffee.prototype.addCondiments = function () {
console.log('给咖啡加糖加牛奶');
};
Coffee.prototype.customerAddCondiments = function () {
return window.confirm('请问需要调料吗?');
}
var coffee = new Coffee();
coffee.init();
})();