Last active
December 26, 2015 23:09
-
-
Save wilmoore/7228490 to your computer and use it in GitHub Desktop.
Strategy vs Pattern Matching (compare to: http://www.dofactory.com/javascript-strategy-pattern.aspx)
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
| -module(greeter). | |
| -export([say_hi/1]). | |
| say_hi({bored}) -> "Sup..."; | |
| say_hi({polite}) -> "Welcome sir."; | |
| say_hi({friendly}) -> "Hey...". | |
| % erl | |
| % c(greeter). | |
| % greeter:say_hi({polite}). | |
| %=> "Welcome sir." |
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
| exports.bored = function () { | |
| this.sayHi = function () { return "Sup..." }; | |
| }; | |
| exports.polite = function () { | |
| this.sayHi = function () { return "Welcome sir." }; | |
| }; | |
| exports.friendly = function () { | |
| this.sayHi = function () { return "Hey..." }; | |
| }; | |
| module.exports = function (type) { | |
| return new exports[type]; | |
| }; | |
| // node | |
| // var greeter = require('./greeter')('polite'); | |
| // greeter.sayHi(); | |
| //=> 'Welcome sir.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment