类设计、接口或具体类
我对接口与具体类的使用有疑问。
我有一个实现一些常见属性/方法的基类。现在我有两个可能的扩展。该基类可以具有一些称为“参数”的属性,或者可以具有另一个称为“子项”的属性,或者可以同时具有这两个属性。
在我看来,我可以创建 2 个接口(IParameterized、IParent),也可以实现三个子类(ParameterObject、ParentObject、ParentParameterObject)。问题是,哪一种设计更好。我目前倾向于接口方法,但我不是一个经验丰富的程序员,所以欢迎任何建议
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
I think your "leaning" is the correct one myself, and others would agree:
http://www.artima.com/lejava/articles/designprinciples4.html