Created
November 5, 2012 20:54
-
-
Save akx/4020282 to your computer and use it in GitHub Desktop.
Abstract Singleton Cache Factory, Coco flavor
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
universe = {} | |
purgatory = {} | |
summon = (type, context) -> (purgatory[type] ||= new universe[type]) <<< {context} | |
summonable = -> universe[it.display-name] = it | |
class Base | |
-> console.log "A new <#{@..display-name}> was born!" | |
summonable class Foo extends Base | |
-> super! | |
shout: -> console.log "My context is #{@context}" | |
summonable class Bar extends Base | |
-> super! | |
shout: -> console.log "MY CONTEXT IS NOT #{@context.to-upper-case!}" | |
(summon \Foo, "derp").shout! | |
(summon \Foo, "durr").shout! | |
(summon \Bar, "hurg").shout! | |
(summon \Bar, "durg").shout! |
Author
akx
commented
Nov 5, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment