RubyFlow The Ruby and Rails community linklog

Ruby Singleton Pattern Again

After giving a bad advice on using Ruby Singleton Pattern in my previous post, I decided to remove it, and write new one. Destroy. Erase. Improve.

Comments

The singleton pattern is a broken pattern. It seems nice at first but it really can hurt. Examples: “hey, lets make database access a singleton pattern! Who needs more than one database anyway?” - I do. When migrating from one database to another for example. Or when connecting different systems. There, BOOM, right in your face. “Hey, let’s make logging a singleton pattern! Who needs more than one logger anyway?” - I do. Different parts of the system have to be logged to different places. Access and Audit logs are not the same. There, BOOM, right in your face. Fact is: you can’t know how I am gonna use it. But: having a ‘default instance’-pattern (don’t know if that’s its real name) can be useful and cover the 90% of the cases where the singleton would have worked too, without making the other 10% unreasonably hard. That is, YourClass.instance returns the default instance instead of the sole instance.

Regards

Oh hey, but kudos at “Destroy. Erase. Improve.” :) It’s good that you don’t let yourself down because of some minor obstacles ;-)

@apeiros: Thanks for your discussion here on Singleton Pattern. Btw, which one were you: AnonymousCritic, CriticOfAnonymousCritic or AnonymousWoodDuck? :)

@dalibor none of them. I didn’t comment on your first try, only pondered to. I try to keep my criticism constructive, also I’m not hiding in anonymity ;-)

Just my 2 cents: if you want a book on design patterns in Ruby, there is one: http://designpatternsinruby.com/ which is pretty good translation of classic design patterns into ruby language and also the “ruby way” :).

Much better this time around. I knew you could do it!

It’s unfortunate you removed your original post and its comments. I often learn as much (or more) from the comments correcting an article with problems as I do from the article itself.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in