views:

54

answers:

3

Hi all,

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 towards the interface method at the moment but im not an experienced programmer so any advise is welcome

+2  A: 

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

Michael Neale
+1  A: 

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.

tijmenvdk
Thanks for the help, Liskov substitution (while a heavy read) is a pretty decent principle!
TerrorAustralis
A: 

If the two extension of base class is only storing additional property than you can create a Generic property returning IList in base class itself than you don't need to create seperate classes.

Follow basic rule of OOP which is 'SIMPLICITY'

saurabh