Created
August 12, 2014 02:36
-
-
Save kotaroito/214c0c9cfeb247d96c9c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var abstractBallFactory = (function() { | |
var types = {}; | |
return { | |
getBall: function(type, customizations) { | |
var constractor = types[type]; | |
return ( constractor ? new constractor(customizations) : null ); | |
}, | |
registerBall: function(type, constractor) { | |
types[type] = constractor; | |
return abstractBallFactory; | |
} | |
}; | |
})(); | |
var Ball = function (customizations) { | |
this.radius = customizations.radius; | |
}; | |
Ball.prototype = { | |
diameter: function() { return this.radius * 2; } | |
}; | |
var SoccerBall = Ball; | |
SoccerBall.prototype = Ball.prototype; | |
(function() { | |
abstractBallFactory.registerBall("Brazuca", SoccerBall); | |
var brazuca = abstractBallFactory.getBall("Brazuca", {radius: 4.3}); | |
console.log(brazuca.diameter()); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment