473,395 Members | 1,941 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,395 software developers and data experts.

Singleton class, use another class?

Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}
}

Thanks for any tips,
Dav
Nov 19 '07 #1
10 1492
Sure. Why wouldn't it be ok?
--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
"davebythesea" <da**********@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...
Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my
Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}
}

Thanks for any tips,
Dav

Nov 19 '07 #2
On Nov 19, 7:49 am, davebythesea
<davebythe...@discussions.microsoft.comwrote:
Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}

}

Thanks for any tips,
Dav
Yes, it is okay to use the Utility class (or any other class for that
matter) if it helps your implementation of the singleton.

As written your singleton is not thread-safe. I would remove the
volatile modifier and wrap the contents of the GetInstance method with
a lock construct. Better yet, create a new SingletonConnection
instance inline with the declaration and have the GetInstance method
just return the reference. Or if you don't require any fancy
initialization or polymorphism then just create a static class.
Nov 19 '07 #3
Since DoStuff() creates a new instance each time, why not just hold
this as a local variable? Unless I missed something in the usage, this
could also be implemented just as a static method. A lot here depends
on whether utility is itself thread-safe...

Note that your SingletonConnection is not guaranteed to be singleton.
First, you might want to seal it, and second there is a race-condition
on the first access; if two threads get past the null check you could
get 2. Unless there is a huge cost associated with initializing a
SingletonConnection (which the ctor doesn't indicate), then perhaps
just use:

private static readonly SingletonConnection instance = new
SingletonConnection();
public static SingletonConnection Instance {get {return instance;}}

Of course, unless the singleton is implementing a specific base /
interface, you might be able to get away with just a static class with
static methods (etc).
Did I miss the point?

Marc
Nov 19 '07 #4


"Brian Gideon" wrote:
On Nov 19, 7:49 am, davebythesea

snip

Yes, it is okay to use the Utility class (or any other class for that
matter) if it helps your implementation of the singleton.

As written your singleton is not thread-safe. I would remove the
volatile modifier and wrap the contents of the GetInstance method with
a lock construct. Better yet, create a new SingletonConnection
instance inline with the declaration and have the GetInstance method
just return the reference. Or if you don't require any fancy
initialization or polymorphism then just create a static class.
Thanks for the tips. I think i saw some code which used the lock construct
so I will probably hunt for it again and add it in there.

Cheers
Nov 19 '07 #5
Hi,

Thanks for your reply.

"Marc Gravell" wrote:
Note that your SingletonConnection is not guaranteed to be singleton.
First, you might want to seal it, and second there is a race-condition
on the first access; if two threads get past the null check you could
get 2. Unless there is a huge cost associated with initializing a
SingletonConnection (which the ctor doesn't indicate), then perhaps
just use:
Would the following be enough to seal the class correctly -

namespace MyApp
{
sealed class SingletonConnection
{

}
}

Cheers,
Dav
Nov 19 '07 #6
On Nov 19, 8:48 am, davebythesea
<davebythe...@discussions.microsoft.comwrote:
Thanks for the tips. I think i saw some code which used the lock construct
so I will probably hunt for it again and add it in there.

Cheers
Unless you have a specific reason for doing manual synchronization I
would stick with what Marc posted.

private static readonly SingletonConnection instance = new
SingletonConnection();
public static SingletonConnection Instance {get {return instance;}}

Nov 19 '07 #7
There is a full discussion of various approaches (pros, cons, etc)
here:

http://www.pobox.com/~skeet/csharp/singleton.html

Marc
Nov 19 '07 #8
yep; actually, this only becomes necessary if there is a non-private
constructor (since you can't inherit from something with only private
constructors), but it is a good idea anyway

Marc
Nov 19 '07 #9
Thanks for this link. Its very useful for me.

David

"Marc Gravell" wrote:
There is a full discussion of various approaches (pros, cons, etc)
here:

http://www.pobox.com/~skeet/csharp/singleton.html

Marc
Nov 21 '07 #10
For the link: you're welcome - there is a lot of good content on that
site on a range of C# topics - definitely recommended reading

For the content: [pings the "thanks" in Jon's direction]

Marc
Nov 21 '07 #11

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

Similar topics

26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
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
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
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...
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++...
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.