Skip to content

Instantly share code, notes, and snippets.

@akx
Created November 5, 2012 20:54
Show Gist options
  • Save akx/4020282 to your computer and use it in GitHub Desktop.
Save akx/4020282 to your computer and use it in GitHub Desktop.
Abstract Singleton Cache Factory, Coco flavor
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!
@akx
Copy link
Author

akx commented Nov 5, 2012

A new <Foo> was born!
My context is derp
My context is durr
A new <Bar> was born!
MY CONTEXT IS NOT HURG
MY CONTEXT IS NOT DURG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment