Results 1 to 3 of 3

Thread: Global "get" functions through with Singleton, is it a good idea?

  1. #1
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Global "get" functions through with Singleton, is it a good idea?

    Hello everybody..

    I'm here again looking for some advices, so thank you before everything.

    I have some collections in my main application class, but I need to retrieve them from other different classes and I would like to know a good way to achieve that. It looks like this:

    Qt Code:
    1. class App : QApplication {
    2. // This class handles the collections and is responsible of setting up them
    3. private:
    4. DualCollection<Unit> units; //Internally implemented as two QHashes with QString as key type. Don't think it really matters.
    5. DualCollection<GameItem> items;
    6. // So on.
    7. };
    8.  
    9. class RosterUnit {
    10. public:
    11. void RosterUnit(); // This method need to get a read-only Unit and parse some information. It also needs access to some items
    12. };
    13.  
    14. class ObjectAction {
    15. public:
    16. void doAction() = 0; // Some subclasses will eventually need access to some information like Units or Items
    17. };
    To copy to clipboard, switch view to plain text mode 

    In fact there are probably other two classes that need to retrieve some information.. I was thinking to use the Singleton pattern with the application class and provide functions like getItem(const QString& key), getUnit(...) and so on, but I'm not sure if it's a good choice. I have read some disadvantages that apply to global variables and singletons like they are hard to debug because they can be modified in the whole application, but it does not apply here because I will provide only a public interface for reading.

    What do you think?

    Thank you very much!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Global "get" functions through with Singleton, is it a good idea?

    If you write proper code, there is no need to debug ;-)

    For your situation is a singleton just fine. What is the alternative? Passing pointers/references around your application. Well I would say that that makes your code much more unreadable and hard to debug.

    And do you really need to subclass QApplication and QHash? Normally one shouldn't subclass Qt basic types line QHash, QMap, QList etc. unless you are really know what you are doing.

  3. #3
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global "get" functions through with Singleton, is it a good idea?

    Uhhh.. I'm sorry.. I meant App is derived from QMainWindow..

    I actually don't subclass QHash.. DualCollection uses two instances inside..

    Thank you
    Last edited by dancas2; 3rd May 2012 at 19:56.

Similar Threads

  1. Replies: 3
    Last Post: 13th July 2011, 16:12
  2. Replies: 0
    Last Post: 13th February 2010, 15:15
  3. Replies: 1
    Last Post: 16th July 2009, 02:25
  4. qobject_cast "good coding practice"
    By janus in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2008, 12:02
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.