并行继承层次结构真的是代码味道吗? [英] Is parallel inheritance hierarchies really a code smell?

查看:130
本文介绍了并行继承层次结构真的是代码味道吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在实践中避免并行层次结构。例如考虑一个必须在不同级别创建/保存/编辑笔记的应用程序,它是一个基于java swing的应用程序。

I can't figure out how to avoid parallel hierarchies in practice. e.g. Consider an application which has to create/save/edit notes on different levels, it's a java swing based application.

域层次结构:

AbstractNote < MonthNote
             < DayNote
             < ProductNote

只读查看层次结构(在JTabPane上显示每个都有JTable以显示特定的注释详细信息)

read only View Hierarchy(shown on JTabPane each having JTable to show the specific note details)

AbstractNotePanel < MonthNotePanel
                  < DayNotePanel
                  < ProductNotePanel

备注编辑器层次结构(当用户点击表格行中的特定注释时显示。

notes editor hierarchy(shown when user clicks on on a particular note in a table row.

AbstractNoteEditorDialog  < MonthNoteEditorDialog
                          < DayNoteEditorDialog
                          < ProductNoteEditorDialog  

我读过一种避免这种情况的方法是使用访客模式。这里
但这似乎不适用于我的情况。

I read one way to avoid this is to use Visitor Pattern.here But this doesn't seem to be applicable to my situation.

我很开心我的上述设计,因为大多数常见代码都是抽象类(在那里应用模板方法模式)。如果我必须创建一个新类型的注释,例如YearNote,那么我必须并行创建YearNotePanel和YearNoteEditorDialog,这是完全可以接受的对我而言,由于模板化,我不需要编写很多代码。最重要的是我想避免代码气味设计请在上面的场景中建议我替代设计,以便我的代码仍然是模块化的。
谢谢

I'm quite happy with my above design as most of the common code is in abstract class(applying template method pattern there). In case if I have to create a new type of note e.g. YearNote then I have to create YearNotePanel and YearNoteEditorDialog in parallel which is quite acceptable for me cause I don't have to code a lot due to templating. Above all I want to avoid code smell in my design. Please suggest me alternate design in above scenario so that my code would be still modular. Thanks

推荐答案

说首先我想避免设计中的代码味道是一个值得称赞的目标但总是请记住,设计的核心是权衡成本

Saying "above all I want to avoid code smell in my design" is a commendable goal but always keep in mind that the core of design is to weigh costs.

在我的应用程序中,我通常有模型类和配置。配置可以在模型中(注释,内省方法)或作为额外文件。

In my applications, I usually have model classes and configuration. Configuration can be in the model (annotations, introspection methods) or as extra files.

UI层读取此配置,然后从中构建UI。这意味着我的设计如下所示:

The UI layer reads this config and then build the UI from it. That means my design looks like this:

AbstractNote < MonthNote
             < DayNote
             < ProductNote

NotePanel
NoteEditorDialog

这通常有效,因为对话框和面板非常通用。当然,我的通用UI层非常复杂,因为它必须处理每个角落的情况。因此,如果您只是查看文件数量或继承层次结构,这可能看起来比您的设计好得多,我的UI层内部的代码比您的更复杂。

This often works because the dialogs and panels are pretty generic. Of course, my generic UI layer is pretty complex because it has to handle each corner case. So while this may look like a much better design than yours if you just look at the number of files or the inheritance hierarchy, the code inside of my UI layer is much, much more complex than yours.

此外,如果我在UI层添加一个功能/修复错误,我可能会在其他地方破坏某些内容(因此需要更改代码才能进行编辑 DayNote 更舒服可以打破 MonthNote )。

Additionally, if I add a feature / fix a bug in the UI layer, chances are much higher that I break something elsewhere (so a change in the code to make editing DayNote more comfortable can break MonthNote).

所以除非你觉得你有很多您可以轻松避免的代码重复或维护成本很高,或者您只有一些模型类型(而不是500),您的设计没有任何问题。

So unless you feel that you have a lot of code duplication that you could avoid easily or maintenance costs are high or you just have a few model types (and not 500), there is nothing wrong with your design as such.

这篇关于并行继承层次结构真的是代码味道吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆