473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 892

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
10
by: Frank Millman | last post by:
Hi all I recently posted a question about subclassing. I did not explain my full requirement very clearly, and my proposed solution was not pretty. I will attempt to explain what I am trying to...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.