Design Patterns - Singleton
by Caleb Tennis
I wanted to show that same pattern using Ruby:
Using Ruby's powerful Mixin functionality, we bring the singleton code into our class definition. This automatically makes our class's new method private, and give us an instance method we can use to get our object:
irb(main):005:0> a = Hermit.new
NoMethodError: private method `new' called for Hermit:Class
irb(main):006:0> a = Hermit.instance
That was easy, wasn't it?
We should really consider building a catalog website translating most of the patterns. I've thought a lot about it and I think it could be a sensational community resource. I would be more than willing to help or maybe even spearhead a project like this, just as soon as I finish editing this book... :)
|I haven't read through all of them, but this looks pretty good.|
|Awesome! Thats really cool. :)|
Wow, I had no idea such a great resource already existed. Thanks for the link!