Created
March 8, 2013 17:03
-
-
Save mikaa123/5117993 to your computer and use it in GitHub Desktop.
Strategies and template methods
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
// Since the GreetingStrategy#execute method uses methods to define its algorithm, | |
// the Template Method pattern, we can subclass it and simply override one of those | |
// methods to alter the behavior without changing the algorithm. | |
var PoliteGreetingStrategy = function() {}; | |
PoliteGreetingStrategy.prototype = Object.create(GreetingStrategy.prototype); | |
PoliteGreetingStrategy.prototype.sayHi = function() { | |
return "Welcome sir, "; | |
}; | |
var FriendlyGreetingStrategy = function() {}; | |
FriendlyGreetingStrategy.prototype = Object.create(GreetingStrategy.prototype); | |
FriendlyGreetingStrategy.prototype.sayHi = function() { | |
return "Hey, "; | |
}; | |
var BoredGreetingStrategy = function() {}; | |
BoredGreetingStrategy.prototype = Object.create(GreetingStrategy.prototype); | |
BoredGreetingStrategy.prototype.sayHi = function() { | |
return "sup, "; | |
}; | |
var politeGreeter = new Greeter(new PoliteGreetingStrategy()); | |
var friendlyGreeter = new Greeter(new FriendlyGreetingStrategy()); | |
var boredGreeter = new Greeter(new BoredGreetingStrategy()); | |
politeGreeter.greet(); //=> 'Welcome sir, Goodbye.' | |
friendlyGreeter.greet(); //=> 'Hey, Goodbye.' | |
boredGreeter.greet(); //=> 'sup, Goodbye.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment