龙继承层次 [英] Long inheritance hierarchy

查看:137
本文介绍了龙继承层次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很长的类继承层次。例如:

I have very long class inheritance hierarchy. For example:

-MyAbstractObject
--MyAbstractUiObject
---MyAbstractTable
-----MyAbstractPageableTable
-------MyAbstractScrollableTable
---------MyAbstractStateblaTable

等等......

etc...

我在 $ C $ READ C完成这个理想的继承深3.有时候,容许使继承深7-9。但我有很深的继承11!

I read at Code complete that ideal inheritance deep is 3. And sometimes it allowable to make inheritance deep 7-9. But I have inheritance deep 11!

如何我可以改变我的架构?什么样的设计模式,适用于我的情况?什么是坏的是,我可以在继承层次改变 MyAbstractPageableTable MyAbstractScrollableTable 的地方。因为我的目标是单一的责任,这2类不混为一体。此外,我想为用户提供不同的接口(API)

How I can change my architecture? What design pattern is applicable to my case? And what is bad is that I can change places of MyAbstractPageableTable and MyAbstractScrollableTable in inheritance hierarchy. This 2 classes not mixed into one because my goal is single responsibility. Also I want to provide for users different interfaces (APIs)

推荐答案

通常最好是使用战略,模式,而不是为每个用例的子类。但是,很难给出任何硬的意见,因为这要看情况而定。

Often it is better to use a Strategy-Pattern and not create an Subclass for each use case. But it is hard to give any hard advice because it depends on the circumstances.

在你的榜样,我猜你可以做一个表实施和给它一个策略对象处理例如Pagenation或任何其他显示策略表应该支持。

In your example I would guess you could do a Table Implementation and give it an strategy-object that handles for example the Pagenation or any other display strategy the table should support.

据约书亚·布洛克的有效的Java往往是最好使用成分过的传承。我不认为更大的传承深度是坏,只要他们留理解的,与11级我想这就是不是这样的。

According to Joshua Bloch's "Effective Java" it is often better to use composition over inheritence. I don't think larger inheritence depths are bad, as long as they stay understandable, with 11 levels I would guess thats not the case.

这篇关于龙继承层次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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