l**********o 发帖数: 22 | 1 我现在有一个比较大的class structure大致是这样的
A (abc)
|
B
/ \
C D
/ \ |
E F G
/ \ |
H I J
现在要做一个新的class,从project的概念上讲是和class B大概是类似eclipse和
circle的关系,但是就具体内容而言,也就是里面的一小部分function不一样,但是
new class其他大部分function和B 一样,它的derive结构也一样,只是它的相对应的
derived class也和B 的每一个derived class几乎一样只有少部分function不同。 在
这种情况下,我该怎么设计这个new class呢?我也不想把大段的code直接copy,但又
想不出怎么安排这个new class和现有class,有经验的请指点一下,谢谢! | g*****g 发帖数: 34805 | 2 Pull out the similar functions from the class hierachy, call it
A', B', C' etc. Let B and your new class extends B'.
Also consider composition over inheritance, a deep hierachy is
often not the right apporach.
【在 l**********o 的大作中提到】 : 我现在有一个比较大的class structure大致是这样的 : A (abc) : | : B : / \ : C D : / \ | : E F G : / \ | : H I J
| l**********o 发帖数: 22 | 3 thanks for the reply!
Do you mean extend just from each level? Say newB class extends from B',
then new C class extends from C'? then the relation between newB and newC is
broken.
How to re-composition deep hierachy? Could you please give me a simple
example, is there any good method other than if-else? thanks
【在 g*****g 的大作中提到】 : Pull out the similar functions from the class hierachy, call it : A', B', C' etc. Let B and your new class extends B'. : Also consider composition over inheritance, a deep hierachy is : often not the right apporach.
|
|