{"id":8,"date":"2008-05-22T17:43:00","date_gmt":"2008-05-22T17:43:00","guid":{"rendered":"http:\/\/www.andypalmer.com\/blog\/?p=8"},"modified":"2009-05-22T13:10:14","modified_gmt":"2009-05-22T13:10:14","slug":"singletons","status":"publish","type":"post","link":"https:\/\/andypalmer.com\/2008\/05\/singletons\/","title":{"rendered":"Singletons"},"content":{"rendered":"
I’ve been playing about with a code base that has a large number of singletons, for what appears to be no apparent reason.
Something about singletons doesn’t sit quite right with me, but in the most part, if they’re not misbehaving, I’ll probably leave them be.<\/p>\n
The thing that bothers me a lot more though, is the fact that everyone else is forced to know about your singleton-ness..<\/p>\n
*shivers*<\/p>\n If you have to store global state, it would be nice if we could hide that from the clients. Why do we have a single instance with instance variables accessed through a static getInstance method, when we could hide (ooh, encapsulation) the implementation behind multiple instances with private static variables? OK, I’m rambling… am I missing something? (please rant below)<\/p>\n","protected":false},"excerpt":{"rendered":" I’ve been playing about with a code base that has a large number of singletons, for what appears to be no apparent reason.Something about singletons doesn’t sit quite right with me, but in the most part, if they’re not misbehaving, I’ll probably leave them be. The thing that bothers me a lot more though, is […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,17,21],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":1,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/8\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}ImASingleton.getInstance().doSomething();<\/code><\/p>\n
By using instance methods, we also get all the interface \/ testing \/ mocking goodness.<\/p>\nnew ImASingletonButYouCantTell().doSomething();<\/code><\/p>\n