Created
October 28, 2014 05:04
-
-
Save rochnyak-d-i/b2d47a00caac0380aaf8 to your computer and use it in GitHub Desktop.
JS шаблон декоратор
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//первый способ | |
function StripedBall( ball ) { | |
this._ball = ball | |
} | |
StripedBall.prototype = { | |
constructor: StripedBall, | |
draw: function() { | |
this._ball.draw(); | |
//и еще какие то действия | |
}, | |
inc: function() { | |
return this._ball.inc(); | |
//и еще какие то действия | |
} | |
} | |
var ball = new SpeckledBall( new StripedBall( new Ball({ radius:100, color:"red"}))); | |
//второй способ | |
function MakeStripedBall( ball ) { | |
var function_name = "draw"; | |
var prev_func = ball[ function_name ]; | |
ball[ function_name ] = function() { | |
prev_func.apply( this, arguments ) | |
console.log("and with stripes"); | |
}; | |
return ball; | |
} | |
var ball = MakeStripedBall( MakeSpeckledBall( new Ball({ radius: 150, color: "blue" }))); | |
//способ третий | |
var tree = {}; | |
tree.decorate = function () { | |
console.log('Make sure the tree won\'t fall'); | |
}; | |
tree.getDecorator = function (deco) { | |
tree[deco].prototype = this; | |
return new tree[deco]; | |
}; | |
tree.RedBalls = function () { | |
this.decorate = function () { | |
this.RedBalls.prototype.decorate(); | |
console.log('Put on some red balls'); | |
} | |
}; | |
tree.BlueBalls = function () { | |
this.decorate = function () { | |
this.BlueBalls.prototype.decorate(); | |
console.log('Add blue balls'); | |
} | |
}; | |
tree.Angel = function () { | |
this.decorate = function () { | |
this.Angel.prototype.decorate(); | |
console.log('An angel on the top'); | |
} | |
}; | |
tree = tree.getDecorator('BlueBalls'); | |
tree = tree.getDecorator('Angel'); | |
tree = tree.getDecorator('RedBalls'); | |
tree.decorate(); | |
//способ номер четыре | |
function Sale(price) { | |
this.price = price || 0; | |
} | |
Sale.prototype.getPrice = function() { | |
return this.price; | |
} | |
Sale.decorators = {}; | |
Sale.decorators.fedtax = { | |
getPrice: function() { | |
var price = this.uber.getPrice(); | |
price += price * 5 / 100; | |
return price | |
} | |
} | |
Sale.prototype.decorate = function(decorator) { | |
var | |
F = function() {} | |
, overrides = this.constructor.decorators[decorator] | |
, i | |
, newObj | |
; | |
F.prototype = this; | |
newObj = new F(); | |
newObj.uber = F.prototype; | |
for(i in overrides) { | |
if(overrides.hasOwnProperty(i)) { | |
newObj[i] = overrides[i]; | |
} | |
} | |
return newObj; | |
} | |
var sale = new Sale(100); | |
sale.decorate('fedtax'); | |
sale.getPrice(); | |
//пятый метод | |
function Sale(price) { | |
this.price = price || 0; | |
this.decorators_list = []; | |
} | |
Sale.prototype.getPrice = function() { | |
var | |
price = this.price | |
, i | |
, max = this.decorator_list.length | |
, name | |
; | |
for(i = 0; i < max; i += 1) { | |
name = this.decorator_list[i]; | |
price = Sale.decorators[name].getPrice(price); | |
} | |
return price; | |
} | |
Sale.decorators = {}; | |
Sale.decorators.fedtax = { | |
getPrice: function() { | |
var price = this.uber.getPrice(); | |
price += price * 5 / 100; | |
return price | |
} | |
} | |
Sale.prototype.decorate = function(decorator) { | |
this.decorator_list.push(decorator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Четвертый способ не рабочий
var sale = new Sale(100);
sale.decorate('fedtax');// цена 100 долларов
sale.decorate('quebec');// добавить федеральный налог
sale.decorate('money');// форматировать как денежную сумму
console.log(sale.getPrice());// “$112.88”
112,88 не получится