How to merge the same method of different classes in the clean way
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to merge the same method of different classes in the clean way

  1. #1
    Join Date
    Dec 2011
    Posts
    4

    How to merge the same method of different classes in the clean way

    Hi everybody..

    I'm developing an application using Qt, but I need some advices regarding sharing class members.
    I am working with some collections (QHash) of project-specific structs and I created a polymorphic class to manage those collections. A derived class also manage some UI components (shared through pointers or references) so it can automatically represent those structs in the UI.. It's something like this:

    Main class

    Code:
    class ArmyEditor : public QMainWindow
    {
    //.... some specific functions ...
    
    private:
        Ui::ArmyEditor *ui;
    
        // Specific structs
        QHash<QString, GameCategory> categories;
        QHash<QString, Column> columns;
        QHash<QString, GameProperty> properties;
        QHash<QString, UnitOption> commonOptions;
        QHash<QString, UnitOption> inheritedOptions;
        QHash<QString, GameItem> items;
        QHash<QString, GameItem> inheritedItems;
        QHash<QString, GlobalText> globalTexts;
        QHash<QString, GlobalText> inheritedGlobalTexts;
        QHash<QString, Unit> units;
    };
    Base class for collection managing..

    Code:
    class StructManager : public QObject {
        Q_OBJECT
    public:
    
        explicit StructManager(QWidget* parent = 0);
    
        // ...Functions that perform actions in shared components...
    
    protected:
        QWidget *parent;
        QHash<QString, GameCategory> *categories;
        QHash<QString, Column> *columns;
        QHash<QString, GameProperty> *properties;
        QHash<QString, UnitOption> *commonOptions;
        QHash<QString, GameItem> *commonItems;
        QHash<QString, GlobalText> *globalTexts;
        QHash<QString, Unit> *units;
    };
    Derived class for UI management and so on

    Code:
    class StructEditor : public StructManager
    {
        Q_OBJECT
    public:
    
        StructEditor(QWidget* parent = 0);
    
        // ...Overriden functions to automatically represent structs in the shared members..
    
    protected:
        QTreeWidget *catList;
        QListWidget *colList;
        QTreeWidget *propList;
        QTreeWidget *optList;
        QListWidget *optActionList;
        QTreeWidget *itemList;
        QListWidget *itemActionList;
        QTableWidget *globalTextsGrid;
        QTreeWidget *unitTree;
        QComboBox *optCategory;
        QComboBox *itemCategory;
        QComboBox *unitCategory;
        QComboBox *optAmountColumn;
        QComboBox *optSetColumn;
    };
    And I share some UI members in the constructor of the MainWindow class..

    Code:
    ArmyEditor::ArmyEditor(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::ArmyEditor)
    {
        ui->setupUi(this);
        
        // Setup Army Struct Manager
    
        armyManager = new StructEditor(this);
    
        armyManager->setCatList(ui->catList);
        armyManager->setOptList(ui->optList);
        armyManager->setOptActionList(ui->optActionList);
        armyManager->setItemList(ui->itemList);
        armyManager->setItemActionList(ui->itemActionList);
        armyManager->setGlobalTextsGrid(ui->globalTextsGrid);
        armyManager->setUnitTree(ui->unitTree);
        armyManager->setOptCategory(ui->optCategory);
        armyManager->setItemCategory(ui->itemCategory);
        armyManager->setUnitCategory(ui->unitCategory);
        armyManager->setOptAmountColumn(ui->optAmountColumn);
        armyManager->setOptSetColumn(ui->optSetColumn);
        armyManager->setCategories(&categories);
        armyManager->setOptions(&commonOptions);
        armyManager->setItems(&items);
        armyManager->setGlobalTexts(&globalTexts);
    
        //.. some other code ..
    };
    I call the functions from the StructEditor class when I need to add a new Category or something like that..
    My project consists of three applications, those of which use almost the same methods for managing these structs, so I decided to use a class with the methods to add, update, remove and represent the structs in the UI sharing some member pointers with the MainWindow class. But now I'm thinking it is a bit dirty and I should not share these members because the MainWindow loses control over them. I was thinking I could create the collections of my structs in the Base class and make a method so I can read (securely) members of those collections in the MainWindow class, but my problem is with UI members. I could use signals and manage those members directly in the MainWindow class, but then I would have to duplicate a lot of code and it would complicate (a bit) the code changes, which is the main reason I decided to unify those methods in a class.

    So, my question is: Is there any way to 'unify' those methods without having to share members or using ugly global variables? I would like to have those methods in a separated file.

    Thanks and greetings!

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: How to merge the same method of different classes in the clean way

    Quote Originally Posted by Danielc View Post
    My project consists of three applications, those of which use almost the same methods for managing these structs, so I decided to use a class with the methods to add, update, remove and represent the structs in the UI sharing some member pointers with the MainWindow class. But now I'm thinking it is a bit dirty and I should not share these members because the MainWindow loses control over them. I was thinking I could create the collections of my structs in the Base class and make a method so I can read (securely) members of those collections in the MainWindow class, but my problem is with UI members. I could use signals and manage those members directly in the MainWindow class, but then I would have to duplicate a lot of code and it would complicate (a bit) the code changes, which is the main reason I decided to unify those methods in a class.
    Seems like you are mixing GUI code with your application logic, i.e. there is no clear separation between GUI and non-GUI design/code. That's why you have to pass so many pointers to other classes. If you instead put all your data and application logic in a class (or set of classes) that are totally independent of the GUI, all you need in your GUI classes is a pointer to the non-GUI class(es).

    Have a look at the Model-View-Controller pattern and it's many variants. They differ in the way that user actions are handled and relayed to the model and view, but they all have a model that contains no GUI code.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2008
    Posts
    726

    Re: How to merge the same method of different classes in the clean way

    dont post same thread in multiple places, please

    http://www.codeguru.com/forum/showthread.php?t=519351

  4. #4
    Join Date
    Dec 2011
    Posts
    4

    Re: How to merge the same method of different classes in the clean way

    I'm sorry.. I will report this post to be deleted. Maybe I thought I hadn't posted it here.

    Maybe the moderator con merge D_Drmmr's answer with the other post and delete this.

    Greetings!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center