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

Implement Singleton Using or Not Using dynamic object.

hi,

my friend ask me the following question. even thogth i prefer the
solution 3, but i owe an explanation.
-------------------------------

I create three versions for singletone pattern below, plz compare them
with advantage and disadvantage:

1.

MyApp.hpp
class MyApp
{
public:
static MyApp& getInstance() { return appInstance; }

private:
MyApp() {;}

private:
static MyApp appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance;

2.

MyApp.hpp
class MyApp
{
public:
static MyApp* getInstance() { return &appInstance; }

private:
MyApp() {;}

private:
static MyApp appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance;
3.

MyApp.hpp
class MyApp
{
public:
static MyApp* getInstance();

private:
MyApp() {;}

private:
static MyApp* appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance==NULL;

MyApp::MyApp()
{
}

MyApp* MyApp::getInstance()
{
if (appInstance == NULL)
appInstance = new MyApp();

return appInstance;
}
---------------------------------------

Dec 29 '05 #1
2 2111
Steven Woody wrote:
hi,

my friend ask me the following question. even thogth i prefer the
solution 3, but i owe an explanation.
-------------------------------

I create three versions for singletone pattern below, plz compare them
with advantage and disadvantage:

1.

MyApp.hpp
class MyApp
{
public:
static MyApp& getInstance() { return appInstance; }

private:
MyApp() {;}

private:
static MyApp appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance;

2.

MyApp.hpp
class MyApp
{
public:
static MyApp* getInstance() { return &appInstance; }

private:
MyApp() {;}

private:
static MyApp appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance;
3.

MyApp.hpp
class MyApp
{
public:
static MyApp* getInstance();

private:
MyApp() {;}

private:
static MyApp* appInstance;
};

MyApp.cpp

MyApp MyApp::appInstance==NULL;

MyApp::MyApp()
{
}

MyApp* MyApp::getInstance()
{
if (appInstance == NULL)
appInstance = new MyApp();

return appInstance;
}
---------------------------------------


If you're singleton needs to be access from multiple translation units
before calling main(), then I recommend a reference return type of
option 3.
It's safer to return a reference then to return a pointer, because it's
easier to accidently delete a return pointer.

//MyApp.hpp
class MyApp
{
public:
static MyApp& getInstance();
private:
MyApp() {}
static MyApp* appInstance;
};

//MyApp.cpp
MyApp MyApp::appInstance==NULL;

MyApp& MyApp::getInstance()
{
if (appInstance == NULL)
appInstance = new MyApp();

return *appInstance;
}

Dec 29 '05 #2
On Wed, 28 Dec 2005 17:41:46 -0800, Steven Woody wrote:
hi,

my friend ask me the following question. even thogth i prefer the
solution 3, but i owe an explanation.
-------------------------------

I create three versions for singletone pattern below, plz compare them
with advantage and disadvantage:

<snip>

4.

MyApp.hpp

class MyApp
{
public:
static MyApp& getInstance()
{
static MyApp appInstance;
return appInstance;
}

private:
MyApp() {;}
};

Doesn't get constructed until you call it the first time (so you don't
have to worry about initialization order), and it will be destructed
automatically when the program exits.

- Jay

Dec 29 '05 #3

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

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
7
by: phl | last post by:
hello, My project in a web project. I choose to use singleton in one of my projects. Towards the end I realise that I seemed to have refered to the fields singleton in my other classes in my...
0
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
2
by: Daniel Kay | last post by:
Hello, currently I am reading the book "Effective C++ Third Edition" from Scott Meyers. While Reading Item 4 (Make sure that objects are initialized before they're used) I got an idea how to...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.