471,330 Members | 1,150 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Subclassing A Singleton

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
0 838

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by BJörn Lindqvist | last post: by
7 posts views Thread by Tim Clacy | last post: by
10 posts views Thread by E. Robert Tisdale | last post: by
3 posts views Thread by Alicia Roberts | last post: by
3 posts views Thread by Harry | last post: by
5 posts views Thread by Pelle Beckman | last post: by
10 posts views Thread by Frank Millman | last post: by
3 posts views Thread by stevewilliams2004 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.