I want to design a class that will be like a singleton in the way that there will be a single main instance of that class, but there can also be multiple clones of the main instance. Only 1 class will be allowed to create the main instance and everybody else can create a clone. Something like this (c#):
class Singleton
{
private static Singleton _mainInstance;
private Singleton() {..}
public void Clone() {..}
public static Singleton MainInstance
{
if (_mainInstance == null)
{
_mainInstance = new Singleton(); // how to secure this for only 1 class?
}
return _mainInstance;
}
}
class MainClass
{
public MainClass()
{
Singleton.MainInstance ....
}
}
MainClass should be the only class that is allowed to instantiate the singleton. In C++ this could be accomplished by hiding the creation logic completely and having MyClass as a friend of Singleton.