简体   繁体   中英

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

我认为您本人的“学习”是正确的,其他人也会同意: 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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM