开发者

Is it better to have one class for a new item dialog and an edit item dialog or different classes?

I'm making a program in which I have a list of items. You can add a new item or you can edit an existing one. The dialog window for editing an item and adding a new one is practically the same, one small detail that changes is that well, one edits and the other one adds the item, very little else is different.

I was wondering if it's generally a better design choice to have two separate classes or to have only one class to which开发者_高级运维 a parameter is passed that specifies whether the dialog is for editing or for adding.

I'm using C++ with Qt but I think the same design choice would apply to any OOP language/framework.

Thanks in advance.


I usually use the same MVC classes for both CREATE and EDIT mode. The model holds a flag to indicate which mode. The View would read the flag in the Model to determine which controls to be displayed/enabled etc. The Controller would also read the flag on the Model to determine which service layer method to call when the user submits the form (e.g. update or create).

I find that this is a more maintainable solution because it avoids duplication and it keeps the logic in one place. i.e. view logic in one view as opposed to two, controller logic in one controller as opposed to two....


Normally this kind of thing is organized by the use of templates in some markup language (the "View" in MVC - often using HTML or XML). I actually don't know much about Qt, but perhaps your classes actually behave as templates.

You would probably have 3 templates:

  1. The Add item template
  2. The Edit item template
  3. The Item Form template

The form template would be imported somewhere in the middle of templates #1 and #2.


I generally only implement an edit dialog. When you want to create a new item, give it a new item to edit. All dialogs should edit private data and return it. That way the clients can respond to OK/Cancel correctly and you don't have to muck with transacting a bunch of UNDO crap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜