Inheritable "singleton" pattern?

  • Thread starter Thread starter Ira Gladnick
  • Start date Start date
I

Ira Gladnick

Would anyone have an idea how to code a base class such that multiple
subclass instances could exist simultaneously, but with the
restriction that there could be only one instance of any particular
subclass at any one time? (My specific need for this relates to form
creation, but seems like a general problem.)

That is, each subclass would be a singleton, but only in regards to
creation of other instances of the exact same subclass.

What I am looking for is a way to code this behaviour entirely in the
base class itself, without requiring the insertion of any
subclass-specific code in the subclasses themselves.

Is there perhaps a way to do this via a clever use of reflection?
 
Hey Ira.
In VB.Net you can use the Shared statement which does just that, creates a
singleton class or method (or both)
I think there's no problem in setting a singleton as a base class but havn't
tryied it yet.
Please let me know if it works

Regards,
Seba
 
Sebastián_UY said:
In VB.Net you can use the Shared statement which does just that, creates a
singleton class or method (or both)

Shared and singleton are very different. They can be used to similar
ends sometimes, but they're not the same thing.
 
Ira Gladnick said:
Would anyone have an idea how to code a base class such that multiple
subclass instances could exist simultaneously, but with the
restriction that there could be only one instance of any particular
subclass at any one time? (My specific need for this relates to form
creation, but seems like a general problem.)

That is, each subclass would be a singleton, but only in regards to
creation of other instances of the exact same subclass.

What I am looking for is a way to code this behaviour entirely in the
base class itself, without requiring the insertion of any
subclass-specific code in the subclasses themselves.

Is there perhaps a way to do this via a clever use of reflection?

If you could guarantee that the subclass had a parameterless
constructor, you could do it with reflection. However, the fact that
the constructor would have to be at least internal poses a problem in
terms of the "singletoneity" - the normal guarantees are lost.

There's also a slight problem in terms of interface - what would you
want the API to this to look like?
 
Hi
Here is the sample code for ur problem

----------------------------------------------------------
using System;

namespace ConsoleApplication21
{
class SingDemo
{
public int x;
public static SingDemo objSD = new
SingDemo();
protected SingDemo()
{
x = 10;
}
public static SingDemo GetInstanceRef()
{
return objSD;
}
}

/// Derived class(From Singleton class SingDemo)
class Derived:SingDemo
{
int y;
public Derived()
{
y = 10;
}
}
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Both objects s1 and s2 will
get same reference
SingDemo s1 =
SingDemo.GetInstanceRef();
SingDemo s2 =
SingDemo.GetInstanceRef();

//Check whether they are pointing
to the same web service or not
s1.x = 123;
Console.Write(s2.x);


/// Declare as many objects u
need for the derived class
Derived d1 = new Derived();
Derived d2 = new Derived();

//Do something with the two
different objects
d1.x = 10;
d2.x = 254;
//both d1 and d2 points to
different objects
Console.Write(d1.x);
Console.Write(d2.x);
Console.ReadLine();
}
}
}
 
Gopinath,

Not clear about this solution.

What I would like is for class "Derived" to also be a singleton,
without having
to add any code to Derived to support this.

In the example, calling Derived.GetInstanceRef() looks like it would
return an instance of SingDemo. What I need is for this method to
return an instance of Derived.

What I'm starting to think is that it might be necessary to add a type
argument to the method that returns an instance ref, so it will know
what kind of subclass to create..
 
If u want ur Derived also to be a singleton provied only
private contructor for the derived class. Follow the same
method which we followed to write the base class.
 
Back
Top