473,473 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

statics inheritance and singletons

I'm implementing a singleton using the example at:
http://www.yoda.arachsys.com/csharp/singleton.html
as a basis (second example).

However - I have about 20 classes I wish to make singletons - and I
don't want to duplicate the code in each one. The difficulty comes in
the method that returns the singleton instance. In the example it's:
if (instance == null)
instance = new Class1();
return instance;

But obviously I don't want it to always return Class1 - if I'm calling
Class2 then I want Class2 returned.

Now, I could just put this code into each class, but it seems wasteful
- is there a more efficient way of doing this?

Thanks,

Andy D

Nov 17 '05 #1
2 1511
In a word, no.

If the classes are completely unrelated then you have no choice but to
duplicate the code in each one.

If they belong in a logical hierarchy (that is, some of them have
"is-a" relationships with the other ones, and no, "is a Singleton"
doesn't count) then you can create a "cheat" singleton that has a
protected constructor rather than a private one. This will allow for
"singleton inheritance", which isn't really singleton any more, but
hey, rules are made to be broken.

For example, one case I can think of for singleton inheritance is if
you want to create a "unit test" version of a class that inherits from
the production version (something in a data layer, for example, which
for test purposes must parrot back canned test data rather than
connecting to a database).

Inheritance doesn't help you at all with the Singleton pattern itself,
because all of the code that implements Singleton is static, and so
doesn't participate in inheritance.

Nov 17 '05 #2
Andrew Ducker <an****@ducker.org.uk> wrote:
I'm implementing a singleton using the example at:
http://www.yoda.arachsys.com/csharp/singleton.html
as a basis (second example).

However - I have about 20 classes I wish to make singletons - and I
don't want to duplicate the code in each one. The difficulty comes in
the method that returns the singleton instance. In the example it's:
if (instance == null)
instance = new Class1();
return instance;

But obviously I don't want it to always return Class1 - if I'm calling
Class2 then I want Class2 returned.

Now, I could just put this code into each class, but it seems wasteful
- is there a more efficient way of doing this?


I'm afraid there isn't - but it's a lot easier if you use the version
which is just:

public static readonly Class1 Instance = new Class1();

Then it's just that line which is duplicated in each class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

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

Similar topics

6
by: Spacen Jasset | last post by:
Since it says in the standard that the order of initialization of variables is indeterminate, does this also apply to a static within a function? e.g. int GetInt() { static int test = 458;...
7
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a...
13
by: Stampede | last post by:
I woundered if the following would be possible: I want to create an abstract Singleton class, which implements the singleton behaviour with the limitation, that the unique object will not be...
6
by: Andrew Ducker | last post by:
Let's say I have a root class called RootBusinessService and I then want to have 25 business service classes based off of it. And each class has a property that's shared between all instances of...
9
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
3
by: Peter | last post by:
Hi I can create types from dynamic loaaded assemblies (Assembly.Load) with Assembly.GetTyp( <typename) - OK I need a reference to the assembly (just Type.GetType() do not work ) now my...
3
by: Jaco Naude | last post by:
Hi I am trying to think of an efficient way of implementing a logging system for a big program. My thoughts are to create a message logging base class and letting all objects that need logging...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.