帮我避免多重继承。帮助我得到正确的oo设计 [英] Help me to avoid multiple inheritance aka. help me to get proper oo design
本文介绍了帮我避免多重继承。帮助我得到正确的oo设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
LightDimDip:extends LightB
。现在我需要将DimDip功能应用于lightB。 任何人都可以提出良好的OOP设计。
再次提醒您,我无法修改LightA。
解决方案
灯是否有通用的界面?在这种情况下,我想你可以在这里使用装饰图案
interface ILight {int GetIntensity()}
class LightA :ILight {int GetIntentisy(){return 10; }}
class LightB:ILight {int GetIntensity(){return 15; }}
class DimLight:ILight {
public DimLight(ILight inner){}
//添加行为inner
int GetIntensity(){return inner.GetIntensity()/ 2;
}
ILight dimLightA = new DimLight(new LightA())
ILight dimLightB = new DimLight(new LightB())
然后,您可以将Light行为放在DimLight类中,并将其应用于实现ILight的任何类。
I have two classes LightA and LightB. I have the source code of LightB but not that of LightA. I extended LightB with LightDimDip. ie LightDimDip:extends LightB
. Now I need to apply DimDip feature to lightB also.
Can any one suggest good OOP design.
Once again I remind you people that I cannot modify LightA.
解决方案
Do the lights have a common interface? In which case I guess you could use the decorator pattern here
interface ILight { int GetIntensity() }
class LightA : ILight { int GetIntentisy(){ return 10; } }
class LightB : ILight { int GetIntensity(){ return 15; } }
class DimLight : ILight {
public DimLight(ILight inner){}
//Add behaviour inner
int GetIntensity(){ return inner.GetIntensity() / 2; }
}
ILight dimLightA = new DimLight(new LightA())
ILight dimLightB = new DimLight(new LightB())
Then you can put the light behaviour in the DimLight class and apply it to any class which implements ILight.
这篇关于帮我避免多重继承。帮助我得到正确的oo设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文