|
-
December 14th, 2011, 04:54 PM
#1
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!
-
December 16th, 2011, 12:25 PM
#2
Re: How to merge the same method of different classes in the clean way
I see you have asked this in several places and seem to ignore the general advice - your problem is your bad design - it is bad because your gui(s) are highly coupled to your data.
Break the dependencies - program to interfaces
Stop sharing member variables
The rest should fall out in the wash.
-
December 16th, 2011, 12:52 PM
#3
Re: How to merge the same method of different classes in the clean way
 Originally Posted by Danielc
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..
I agree with others who have commented on this.
Unless you're developing a spreadsheet, word processor, or similar program where the user interface is an integral part of the business logic, you shouldn't design programs this way, where your business logic is tightly coupled with the user interface and how it works. Even in those programs I mentioned, spreadsheets, word processors, etc. they have business logic that is not tied to the user interface.
Where is the "backend" portion of the program, the part that actually does the real work, regardless of what the UI looks like? If you're not already familiar with the "Model-View-controller" pattern, I suggest you get familiar with it.
http://en.wikipedia.org/wiki/Model%E...0%93controller
Regards,
Paul McKenzie
-
December 16th, 2011, 12:58 PM
#4
Re: How to merge the same method of different classes in the clean way
Hi Amleto! Glad to see you here.
I understand there is a terrible design flaw, and I want to fix it before continuing. But, in order to fix it, I need to be sure the new design avoids code-duplication. It is the main reason why I'm sharing the UI members. My data is not as UI-dependent as you think. I repeat, the problem is that some of my UI components are managed in the same way in some classes, and I want to avoid code repeat. Someone suggested using a template base class, and it seems to be the cleaner way I can do it.
Thanks and greetings
-
December 16th, 2011, 01:25 PM
#5
Re: How to merge the same method of different classes in the clean way
struct - holds your data
struct manipulator - provides getter/setters. could take a reference to a struct in ctor, or possibly accept a pointer in some kind of init method.
manager - defines some commands (methods) that essentially translates gui changes into struct manipulator calls.
gui editor - contains a manager. slots/signals are connected to manager methods
so, if your gui editors 'do the same thing', you can program everything in one manager, although each different editor should not share the manager.
The only components that contain gui elements are the gui editors.
The struct manipulator and the manager most likely have no member variables.
-
December 16th, 2011, 01:40 PM
#6
Re: How to merge the same method of different classes in the clean way
I don't like the idea of sharing many references, so I think it would be better if I create a StructCollection (for each struct) that has a QHash<QString, Struct> and provide add/update/remove/get methods.
I never said my GUI editors do the same thing, I only said that some UI components are managed in exactly the same way. Anyway, yes, I could merge two projects, but still there would be two forms who would need to do almost the same thing (one would add some options). This seems to be a good time for inheritance, but then I would have to design with code the rest of the form.
Maybe I'm complicating or ignoring something obvious.
Thanks and 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|