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 SingletonConnec tion
{
private volatile static SingletonConnec tion instance;
private Utility utility;
private SingletonConnec tion()
{
}
public static SingletonConnec tion GetInstance()
{
if (instance == null)
{
instance = new SingletonConnec tion();
}
return instance;
}
public void DoStuff()
{
utility = new Utility();
utility.DoStuff ();
}
}
class Utility
{
public Utility()
{
}
public void DoStuff()
{
}
}
}
Thanks for any tips,
Dav 10 1527
Sure. Why wouldn't it be ok?
--
Chris Tacke, eMVP
Join the Embedded Developer Community http://community.opennetcf.com
"davebythes ea" <da**********@d iscussions.micr osoft.comwrote in message
news:24******** *************** ***********@mic rosoft.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 SingletonConnec tion
{
private volatile static SingletonConnec tion instance;
private Utility utility;
private SingletonConnec tion()
{
}
public static SingletonConnec tion GetInstance()
{
if (instance == null)
{
instance = new SingletonConnec tion();
}
return instance;
}
public void DoStuff()
{
utility = new Utility();
utility.DoStuff ();
}
}
class Utility
{
public Utility()
{
}
public void DoStuff()
{
}
}
}
Thanks for any tips,
Dav
On Nov 19, 7:49 am, davebythesea
<davebythe...@d iscussions.micr osoft.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 SingletonConnec tion
{
private volatile static SingletonConnec tion instance;
private Utility utility;
private SingletonConnec tion()
{
}
public static SingletonConnec tion GetInstance()
{
if (instance == null)
{
instance = new SingletonConnec tion();
}
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 SingletonConnec tion
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.
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 SingletonConnec tion 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
SingletonConnec tion (which the ctor doesn't indicate), then perhaps
just use:
private static readonly SingletonConnec tion instance = new
SingletonConnec tion();
public static SingletonConnec tion 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
"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 SingletonConnec tion
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
Hi,
Thanks for your reply.
"Marc Gravell" wrote:
Note that your SingletonConnec tion 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
SingletonConnec tion (which the ctor doesn't indicate), then perhaps
just use:
Would the following be enough to seal the class correctly -
namespace MyApp
{
sealed class SingletonConnec tion
{
}
}
Cheers,
Dav
On Nov 19, 8:48 am, davebythesea
<davebythe...@d iscussions.micr osoft.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 SingletonConnec tion instance = new
SingletonConnec tion();
public static SingletonConnec tion Instance {get {return instance;}}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 was looking into decorators. The examples from PEP 318
http://www.python.org/peps/pep-0318.html#examples
don't work - AFAIK because:
|
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) explicitly make the arbitrary class's constructor and destructor private
b) declare the Singleton a friend of the arbitrary class
|
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
|
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
template <class T>
class Singleton : public T {
public:
static T * Instance();
|
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 what sort of
problems/issues should be considered?
Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?
If someone knows about the...
| |
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++ Design" or similar
books, but I feel there are full of clever guys here
who could help me out.
|
by: Preets |
last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
|
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: \
|
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 is, regardless of where the object is hidden, everyone needs access to it.
The global point of access is the object's Instance() method.
Individual users need to be prevented from creating their own instances of the Singleton.
|
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
constraints/environment are as follows:
1) Everything is single-threaded during static initialization (as in
prior to the open brace of main)
2) The environment may be multi-threaded during nominal program
execution (within {} of main)
3) I...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |