
2011/6/28 Klaim - Joël Lamotte <mjklaim@gmail.com>
On Tue, Jun 28, 2011 at 07:13, Ben Robinson <icaretaker@gmail.com> wrote:
The source is now available on GitHub at:
I started to read it and I have a minor suggestion : would it be a good idea to also allow creation/destruction of the object without using create() and destroy()?
class TheObject : public singularity<TheObject, global_access > {...}
m_the_object = new TheObject(); ...
// in another module auto& the_object = TheObject::get(); // ok if run after the previous code ...
// in another function delete m_the_object; // now TheObject::get() will throw
I've seen such possibility in Singleton implementations and found it useful but I'm not sure if it's a good idea because of the destruction function throwing.
Thank you for your input. I explored your idea and created such a base class. However, I think that trying to combine using new() and delete() to manage the lifetime of an object, and then have the base class provide a static function which returns a reference to the singular instance of the class gets a little messy (subjective opinion).
However, to your point, any class can be made singular by simply adding a private static boolean to their class and properly detecting if an instance already exists inside the constructor. If global access is needed, a global pointer to this instance can be created, for example. I would like to define the Singularity Design Pattern as one in which the user calls T::create() and T::destroy() to explicitly manage the lifetime of the object. It is these explicit calls that makes lifetime management so much simpler with Singularity compared to Singleton. Singleton instead provides T::instance() to get the lazily initialized object, and the lazy initialization, and lack of control with destruction makes lifetime management a headache. Also, because T::get() is disabled by default for Singularity, no global access to the instance is required, as it is for Singleton. This is an additional benefit for those who would prefer to explicitly pass the object to those other object which need access to it. Thank you, Ben Robinson, Ph.D. Joël Lamotte
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost