Sign in to follow this  

Design question

This topic is 4298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, first of all, please don't flame at me. I don't have much experience, and their is nobody here at work who can help me on those questions (apart from saying : "I like it that way, and not this way" without any good argument :/) So, now the question. I have a few classes of my program that I want to have only 1 instance of. At first, I used the singleton pattern. It worked fine, did its job. But I don't really like having to write : CFoo::GetInstance->DoSomething(); all the time :/ And I tried on one classe to do the following : All the members / variables are static, and now it looks like : CFoo::DoSomething(); I really find the second way more readable, easy to use. BUT I wonder if having a class with everything static in it is fine :/ A little voice in my head says that it's very dirty .At least I don't really have control on WHERE the members are initialized. But for some my classes that is not a problem. So the real question is : What would be the different methods to achieve my goals. And most important : why / why not ! I want to learn and not just be told : 'do this it's better than that' Thx in advance. [Edited by - paic on March 3, 2006 10:41:20 AM]

Share this post


Link to post
Share on other sites
A quick comment... When you have a static function in a class like that, it's usually so that you can call the function and have it do whatever independently of the rest of the class's functions/members (kind of like a global function). Is this what you want to do?

Also, read this article, should answer your questions:

http://cplus.about.com/od/beginnerctutorial/l/aa080802c.htm

Share this post


Link to post
Share on other sites
Having an all-static class is basically called a "monostate". It's a viable Singleton alternative, but as you pointed out, you can't really gaurantee initialization order of it's static data. You probably need an Init() in many cases.

If you like the Singleton pattern (and, hey, a lot of people do), but you don't like the ugly syntax, there is a very simple alternative:

If you have:

SomeClass::GetInstance()

you can "shorthand" it with a simple define:

#define Foo SomeClass::GetInstance()

Now in your code you just say

Foo->Whatever();

Share this post


Link to post
Share on other sites
thx leiavoia. Apparently their is nothing really wrong in what I'm doing (well, may cause problems with dlls and threads, but I don't need those for the moment)
And It's not really monostate pattern in my case since even the methods are static (in monostate pattern, they are not static) but it's quite close.

Share this post


Link to post
Share on other sites
Quote:
Original post by paic
thx leiavoia. Apparently their is nothing really wrong in what I'm doing (well, may cause problems with dlls and threads, but I don't need those for the moment)
And It's not really monostate pattern in my case since even the methods are static (in monostate pattern, they are not static) but it's quite close.


You can also define something like this - it will hide how you access your singleton:

class SingletonAccessor
{
public:
MyClass *operator->() { return theSingleton::getInstance(); }
};

It looks like a monostate, but it's not a monostate :) Anyway, it's not much different from

MyClass *myClass = theSingleton::getInstance();
myClass->callFunc();

(which is perfectly legal and safe, providing that your singleton implementation is safe)

Regards,

Share this post


Link to post
Share on other sites

This topic is 4298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this