singleton and other designs

This is a discussion on singleton and other designs within the C++ Programming forums, part of the General Programming Boards category; Hello Im facing some design problems when programming my application.. I have a class options (object) that stores all the ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    singleton and other designs

    Hello

    Im facing some design problems when programming my application..

    I have a class options (object) that stores all the program options and its unique (only one object of this class). Some other classes depend on it (have to be able to access data inside it).

    Now I wonder what is the best approach when designing such programs..

    Should I make 'class options' singleton or is it better to store a reference in each class that needs access to its data? (So I would have class &options; within each class )


    I dont like passing reference of 'class options' to each class that needs to read data from it.

    Which way do you prefer, what are the pros and cons?

    Thanks for help

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    Well, I can't see much reason for having more than one instance for such a class (although maybe someone else can), so, yes, I would implement it as a singleton.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    as long as you're in a single-threaded environment, singleton is fine. If you're in a multi-threaded environment, singleton can cause problems.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by ChaosEngine View Post
    as long as you're in a single-threaded environment, singleton is fine. If you're in a multi-threaded environment, singleton can cause problems.
    you can create singletone that is thread safe. but you are right the classic example is not.

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by l2u View Post
    Should I make 'class options' singleton or is it better to store a reference in each class that needs access to its data? (So I would have class &options; within each class )
    why not use it as global variable?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by kroiz View Post
    why not use it as global variable?
    Maybe, Because global variables create more problems than they solve?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by vart View Post
    Maybe, Because global variables create more problems than they solve?
    While that can be argued, most of the problems of global variables can also be said about singletons. Singletons are variables that have global scope and a single instance per application. If you do not need a single instance, then you may as well use a global variable. If you think it is bad to use variables in the global scope, then you should not be using singletons.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Singleton is the Design Pattern that is over used the most.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    can you provide a reason or real life examples to support this? (true interest)
    what better alternatives are out there to model single resources?

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    why do you need an "Options" class at all? Such a class has low coherence (it contains data relating to many functional areas) and high coupling (it introduces a dependency between unrelated areas). Both of these are Bad Things (tm).

    Why not simply allow the "option" to reside wherever it is most relevant?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    Quote Originally Posted by vart View Post
    Maybe, Because global variables create more problems than they solve?
    The difference between a global variable and a static member of a singleton class is one of scope ONLY. For all intents and purposes, a static member of a class *IS* a global variable.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by brewbuck View Post
    The difference between a global variable and a static member of a singleton class is one of scope ONLY. For all intents and purposes, a static member of a class *IS* a global variable.
    And why to use static members in the singleton? Doesn't it kills the idea of singleton?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    88
    > For all intents and purposes, a static member of a class *IS* a global variable.
    Code:
    class foo
    {
    public:
    	static int* getInstance();
    private:
    	static int* m_instance;
    };
    I'm just curious, how are either of the static members in the above example comparable to global variables?

    My advice is that if you're looking for an object-oriented command line parsing model, look to a purely-OO language like Java to see how it's done there. Really, I have never taken an object-oriented approach to command line parsing before and on second thought, ChaosEngine is probably right about a singleton Options class not being the best from a design standpoint.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by UMR_Student View Post
    > For all intents and purposes, a static member of a class *IS* a global variable.
    Code:
    class foo
    {
    public:
    	static int* getInstance();
    private:
    	static int* m_instance;
    };
    I'm just curious, how are either of the static members in the above example comparable to global variables?

    My advice is that if you're looking for an object-oriented command line parsing model, look to a purely-OO language like Java to see how it's done there. Really, I have never taken an object-oriented approach to command line parsing before and on second thought, ChaosEngine is probably right about a singleton Options class not being the best from a design standpoint.
    If you implemented that in C, you would have to make m_instance global. Actually, in this case, since there is only one method that calls m_instance you could make it a static local variable, but that wouldn't be possible if there are multiple methods that access and modify m_instance.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by ChaosEngine View Post
    Why not simply allow the "option" to reside wherever it is most relevant?
    I wanted to keep all options that are being read from a file (user) in a special class (for instance class user_options).

    I havent thought that might be a bad idea..

    So you're saying it would be better to just copy options to class, where they're needed?
    In this case I would have to store references to all classes that need particular options and then copy them where they belong to..

    Is this a good approach?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21