Re: [boost] Is there interest in an alternative to the Singleton anti-pattern?

Matt, I am glad your interest is piqued (and I learned it is not spelled peaked). I have posted the source file, and a Boost Test unittest file in the Vault in the folder Singularity. Commenting on your first and second point, the Singularity Pattern (as I am calling it) is flexible enough to allow constructing the class with any supplied constructor. It is true you can construct a Singleton using the default constructor, and pass in dependencies via member functions, but you cannot pass in references this way. Singularity provides this flexibility which Singleton does not. I agree with you that the lifetime of Singleton is difficult to manage. The lifetime of a Singularity is bracketed by the calls to create() and destroy(), just like any other object created with new() and delete(). The difference being that new() can create any number of a objects, and Singularity::create() will only allow one. I look forward to your feedback on the pattern. I recommend looking at the usage in the Singularity/singularity_unittest.cpp file before studying the implementation. Thank you, Ben Robinson, Ph.D. On 6/23/2011 1:08 AM, Ben Robinson wrote:
The Singleton pattern does have its uses because it guarantees a single instance of a class. However, it suffers from three weaknesses:
1) Encapsulation: Singleton provides global access to the class. Some may view this as a feature, but advocates of unit testing, and dependency injection would disagree. 2) Initialization: Singleton restricts the class to the default constructor. This makes Singleton unusable for classes which require non-default constructors. 3) Lifetime: Singleton introduces lifetime management complexities (for a detailed discussion, see Chapter 6 of Modern C++ Design by Andrei * Alexandrescu*).
I have formalized what I believe is a novel design pattern, which guarantees a single instance of a class, but which does not suffer 1-3 above. And unlike Monostate, this pattern I propose does instantiate a genuine instance of any class, using any one of its available constructors. I hope this inquiry peaks your interest.
I've often wondered about singleton's bad rap (other than the difficulty of making them reliable in C++). I use a singleton from the boost vault for hiding implementation details of globally accessible interfaces (e.g. an interface to access information about elements of the periodic table when I don't hard-code all the constants). This usage is unit testable as long as the implementation details can be thoroughly tested through the interface. It could support DI too if the dependencies are given to the singleton's methods instead of at construction. I don't understand weakness #2. My usage of singletons makes initialization an implementation detail. Construction is not part of the singleton's interface because constructing it different ways from different parts of the program would mean having more than a single instance. #3 is a indeed a bitch with C++. The boost vault solution I use makes it quite easy to chain singletons though. Nevertheless, my interest is piqued. Thanks, -Matt

Matt,
[snip]
Thank you,
Ben Robinson, Ph.D.
[huge quoted text]
Please don't top-post ( http://en.wikipedia.org/wiki/Posting_style#Top-posting) when you answer on this list. Instead, please use the interleaved style ( http://en.wikipedia.org/wiki/Posting_style#Interleaved_style) or at least the bottom style. Thanks, Philippe p.s: see the wiki page for reasons to do so.

2011/6/24 Ben Robinson <icaretaker@gmail.com>:
I look forward to your feedback on the Singularity Pattern. The unittests in Vault/Singularity/singularity_unittest.cpp are great to study first to see how Singularity is meant to be used.
I have some remarks: 1) class single_threaded - if you remove user defined destructor and constructor, it will become a POD type and compillers will do better optimizations 2) singularity_state, singularity_instance and singularity classes can be combined together if you remove BOOST_PP_REPEAT(BOOST_SINGULARITY_ARGS_MAX, BOOST_PP_DEF_CLASS_TYPE_DEFAULT, _) from singularity and put this templates directly for create functions. If you do so, code like: singularity<Horizon, single_threaded, int, Event*, Event&>::create(3, &event, event); will look much better, just like: singularity<Horizon, single_threaded>::create(3, &event, event); Then, FRIEND_CLASS_SINGULARITY macro will be much shorter 4) May be, it would be much better to derive from singularity. Then the code will look just like this: Horizon::create(3, &event, event); or like: Horizon< single_threaded >::create(3, &event, event); 5) It would be great, to have a get() function in singularity, that returns reference It's just what I saw in a 15 minutes of review. I will make a more deep look a little bit later. Any comments are welcomed. Best regards. Antony Polukhin

Philippe Vaucher wrote: [snip]
Please don't top-post ( http://en.wikipedia.org/wiki/Posting_style#Top-posting) when you answer on this list. Instead, please use the interleaved style ( http://en.wikipedia.org/wiki/Posting_style#Interleaved_style) or at least the bottom style.
More to the point: <http://www.boost.org/community/policy.html#quoting> _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
participants (4)
-
Antony Polukhin
-
Ben Robinson
-
Philippe Vaucher
-
Stewart, Robert