Skip to content

Instantly share code, notes, and snippets.

@rochnyak-d-i
Created October 28, 2014 05:03
Show Gist options
  • Save rochnyak-d-i/572819e1eaf07d26d287 to your computer and use it in GitHub Desktop.
Save rochnyak-d-i/572819e1eaf07d26d287 to your computer and use it in GitHub Desktop.
JS шаблон стратегия
//способ первый
function ShapeFactory(size, color)
{
this.size = size;
this.color = color;
}
ShapeFactory.prototype =
{
constructor: ShapeFactory,
makeCircle: function () { return new Shapes.Circle({ radius: this.size / 2, color: this.color }); },
makeSquare: function () { return new Shapes.Square({ side: this.size, color: this.color }); },
makeTrinagle: function () { return new Shapes.Triangle({ side: this.size, color: this.color }); }
}
//способ второй
function PetFactory() {
};
PetFactory.register = function(name, PetConstructor) {
if (name instanceof Function) {
PetConstructor = name;
name = null;
}
if (!(PetConstructor instanceof Function)) {
throw {
name: 'Error',
message: 'PetConstructor is not function'
}
}
this[name || PetConstructor.name] = PetConstructor;
};
PetFactory.create = function(petName) {
var PetConstructor = this[petName];
if (!(PetConstructor instanceof Function)) {
throw {
name: 'Error',
message: 'constructor "' + petName + '" undefined'
}
}
return new PetConstructor();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment