开发者

Class design, Interfaces or Concrete classes

I have a problem regarding the use of interfaces vs concrete classes.

I have a base class that implements some common properties/methods. Now i have two possible extensions. Either this base class can have some property called Parameters, Or it can have another property called Children, or it can have both.

The way i see it i could create either 2 interfaces (IParameterised, IParent) or i could implement three subclasses (ParameterObject, ParentObject, ParentParameterObject). The question is, which one of these is better design. I'm leaning tow开发者_如何学运维ards the interface method at the moment but im not an experienced programmer so any advise is welcome


I think your "leaning" is the correct one myself, and others would agree: http://www.artima.com/lejava/articles/designprinciples4.html


Some backgrounds on this topic that should get you going: the Liskov substitution principle (more here) and the concept of composition over inheritance.

In short, do not implement the 3 subclasses (i.e., the inheritance approach): what will happen if you need 2 or 3 additional common properties/methods down the road? With just 2 additions you are looking at 15 or so unique combinations. For composition, look into the state and strategy patterns.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜