By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

Subclassing A Singleton

P: n/a
Johns ? got me to thinking so here is some prototype code that attempts
to subclass a singleton.

namespace SubclassSingleton
{
class NoahsArkWithOneMale
{
private Animal[] afa = new Animal[6];
static void Main(string[] args)
{
NoahsArkWithOneMale na = new NoahsArkWithOneMale();
na.afa[0] = FemaleGiraffe.GetInstance();
na.afa[1] = FemaleSnake.GetInstance();
na.afa[2] = FemaleTiger.GetInstance();
//na.afa[2]= OnlyMaleAnimal.GetOnlyMaleAnimal(); //null
na.afa[3] = MaleGiraffe.GetOnlyMaleAnimal();
na.afa[3] = MaleTiger.GetOnlyMaleAnimal();
na.afa[4]= MaleGiraffe.GetOnlyMaleAnimal();
na.afa[5] = OnlyMaleAnimal.GetOnlyMaleAnimal();
foreach (Animal a in na.afa)
{

System.Console.WriteLine(a.GetName()+"("+a.GetSex( )+")");
}
System.Console.ReadLine();
// output G(F) S(F) T(F) G(M) G(M) G(M)
}
}
public abstract class Animal
{
public abstract string GetSex();
public virtual string GetName() { return "Animal"; }
}
// singleton class
public abstract class OnlyMaleAnimal : Animal
{
protected OnlyMaleAnimal() { ;}
static readonly object padlock1 = new object();

private static OnlyMaleAnimal uniqueMA= null;
// can only create one instance of a MaleAnimal
protected static OnlyMaleAnimal UniqueMA
{
get { return uniqueMA; }
set {
lock (padlock1)
{
if (uniqueMA == null)
{
uniqueMA = value;
}
//else do nothing if two threads read null
"simultaneously", only one will write.
// or you could throw here on concurrency conflict
}
}
}

public static OnlyMaleAnimal GetOnlyMaleAnimal()
{
return uniqueMA; // may be null!
}
public override string GetSex() { return "M"; }
}
public sealed class MaleTiger : OnlyMaleAnimal
{
static readonly object padlock = new object();
public new static OnlyMaleAnimal GetOnlyMaleAnimal()
{
lock (padlock)
{
if (UniqueMA == null)
{
UniqueMA = new MaleTiger();
}
return UniqueMA;
}
}
private MaleTiger() { ;}
public override string GetName() { return "Tiger"; }
}
public sealed class MaleGiraffe : OnlyMaleAnimal
{
static readonly object padlock = new object();
public new static OnlyMaleAnimal GetOnlyMaleAnimal()
{
lock (padlock)
{
if (UniqueMA == null)
{
UniqueMA = new MaleGiraffe();
}
return UniqueMA;
}
}
private MaleGiraffe() { ;}
public override string GetName() { return "Giraffe"; }
}
public abstract class FemaleAnimal : Animal
{
public override string GetSex() { return "F"; }
}
public sealed class FemaleGiraffe : FemaleAnimal
{
private static readonly FemaleGiraffe uniqueFG= new
FemaleGiraffe();
static FemaleGiraffe() { ;}
public static FemaleGiraffe GetInstance()
{
return uniqueFG;
}
private FemaleGiraffe() {;}
public override string GetName(){return "Giraffe";}
}

public sealed class FemaleSnake : FemaleAnimal
{
private static readonly FemaleSnake uniqueFS = new
FemaleSnake();
static FemaleSnake() { ;}
public static FemaleAnimal GetInstance()
{
return uniqueFS;
}
private FemaleSnake() {;}
public override string GetName() { return "Snake"; }
}

public sealed class FemaleTiger : FemaleAnimal
{
private static readonly FemaleTiger uniqueFG = new
FemaleTiger();
static FemaleTiger() { ;}
public static FemaleAnimal GetInstance()
{
return uniqueFG;
}
private FemaleTiger() {;}
public override string GetName() { return "Tiger"; }
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.