473,231 Members | 1,621 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,231 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 887

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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.