473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Good design?

Hi,

I've just finished writing a template (whew!) and
would like some opinions on the design.

The goal was to be able to do fairly simple
singletons (no excplicit thread-safety, etc)
and keep it simple.
It's also supposted to give the ability to
do simple derived singletons - which is not
possible with my other attempts that were based
on the classical "global pointer to an object
which is set using 'this' in the objects constructor"
(I hope you understood that...).

Anyway, comments? suggestions?
I'm a bit unsure of the ()-operator - could
that get in the way of other functions, operators, etc?

template<class T>
class Singleton {
public:
T* Instance ();
void Release ();
T* operator() ();

Singleton () {}

private:
static T* m_singleton;
};

template<class T> T* Singleton<T>::m_singleton = 0;

template<class T>
T* Singleton<T>::operator() () {
return this->Instance ();
}

template<class T>
T* Singleton<T>::Instance () {
if (m_singleton == 0) {
m_singleton = new T;
}
return m_singleton;
}

template<class T>
void Singleton<T>::Release () {
if (m_singleton == 0)
return;
delete m_singleton;
m_singleton = 0;
}

The code allows me to do this:

Singleton<MyClass> mysingleton;
mysingleton ()-> AFunction();
mysingleton.Instance ()->AFunction();

which I find is rather nice.

-- Pelle
Jul 23 '05 #1
7 1354
Pelle Beckman wrote:
I've just finished writing a template (whew!) and
would like some opinions on the design. Anyway, comments? suggestions?


Where are the unit tests?

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #2
Phlip skrev:
Pelle Beckman wrote:

I've just finished writing a template (whew!) and
would like some opinions on the design.


Anyway, comments? suggestions?

Where are the unit tests?


I'll guess you'll laugh you head off...

I don't know.
What's a unit test?

-- Pelle
Jul 23 '05 #3
Pelle Beckman wrote:
I've just finished writing a template (whew!) and
would like some opinions on the design.
[...]


Two things are missing: parameterized construction of the instance
or construction of the instance by a factory. I'd probably try to
have a special construction policy, and by default it would just
invoke the default c-tor.

V
Jul 23 '05 #4

"Phlip" <ph*******@yahoo.com> wrote in message
news:e7****************@newssvr31.news.prodigy.com ...
Pelle Beckman wrote:
I've just finished writing a template (whew!) and
would like some opinions on the design.

Anyway, comments? suggestions?


Where are the unit tests?


I have them, and I'm not letting you look at them unless you say "please".
:-)

I think he was looking for suggestions on the *design*, actually. (There
isn't even a "main" there, so talking about unit tests is going a wee bit
beyond the question, I think.)

-Howard
Jul 23 '05 #5
Victor Bazarov skrev:
Pelle Beckman wrote:
I've just finished writing a template (whew!) and
would like some opinions on the design.
[...]

Two things are missing: parameterized construction of the instance
or construction of the instance by a factory. I'd probably try to
have a special construction policy, and by default it would just
invoke the default c-tor.

V


Good point.
How you I go about implementing c-tor parameter passing?
Hints, solutions, hyperlinks?

-- Pelle
Jul 23 '05 #6
Pelle Beckman wrote:
Where are the unit tests?


I'll guess you'll laugh you head off...

I don't know.
What's a unit test?


Google for it.

Contrary to Howard's laugh, tests are a design thing. In the big C++ shops,
a request for a code review shall be accompanied by tests.

Here's one for your thing (with made-up code):

struct Foo{};

TEST_(TestCase, singular)
{
Singleton<Foo> mysingleton;
Singleton<Foo> yoursingleton;
Foo * p1 = mysingleton ();
Foo * p2 = yoursingleton.Instance ();
CHECK_EQUAL(p1, p2);
}

That test shows the Foo really is singular; the system did not allocate two
of them, at different addresses.

One runs all tests after every few edits. If a test fails, you have the
option to either hit Undo until they all pass, or you can debug. Without
tests, you don't know as soon as possible when you broke something, and you
can't use Undo. The only choice is to debug.

Now about style, if your system either throws an exception if new Foo fails,
then your operators can never return NULL. So maybe your system should
return a reference. Always use references without a reason to use pointers.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #7
Pelle Beckman wrote:
Victor Bazarov skrev:
Pelle Beckman wrote:
I've just finished writing a template (whew!) and
would like some opinions on the design.
[...]


Two things are missing: parameterized construction of the instance
or construction of the instance by a factory. I'd probably try to
have a special construction policy, and by default it would just
invoke the default c-tor.

V

Good point.
How you I go about implementing c-tor parameter passing?
Hints, solutions, hyperlinks?


Andrei Alexandrescu, "Modern C++ Design", chapter 6, "Implementing
Singletons".

V
Jul 23 '05 #8

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

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
2
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart...
3
by: Erik De Keyser | last post by:
Hi group, I have a couple of projects with up to 8 forms. On most projects I write the code on each form with no modules. The last project I use 1 module and minimal code on each form , in fact...
15
by: Randall Smith | last post by:
I've been programming in Python for about 2 years. I think it offers the best combination of simplicity and power of any language I have explored. As I write more and larger and complex programs,...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
13
by: John Salerno | last post by:
Here are a few I'm considering: Design Patterns Explained : A New Perspective on Object-Oriented Design (2nd Edition) (Software Patterns Series) by Alan Shalloway Design Patterns C# by...
24
by: Gaijinco | last post by:
I found one of that problems all of us have solve when they begin programming: given 3 numbers print the greater and the lesser one of the set. I was trying to remember the if-then-else...
7
by: TAVOSOFT | last post by:
Hi friends, I am begginer , I wanna to learn VB2005 ,Which are good book for to learn VB2005 of level -begginer-intermediate. Thanks you friends.
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
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
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
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,...
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

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.