Created
October 28, 2014 05:03
-
-
Save rochnyak-d-i/572819e1eaf07d26d287 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 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