简体   繁体   中英

Design Pattern: Inheritance or Composition

I have 2 classes A and B, both of which have properties X and Y; but class B also has another property, Z. Classes A and B are completely unrelated to one another, they just share properties X and Y.

Inheritance

class A
{
    public int X;
    public int Y;
}

class B : A
{
    public int Z;
}

Class B does not have a "is-a" relationship to class A, so it breaks inheritance principals is OOP.

Composition

class A
{
    public int X;
    public int Y;
}

class B
{
    public A ObjA;
    public int Z;
}

Class B does not have a "has-a" relationship to class A so it breaks composition principals is OOP.

Without duplicate code, should I use inheritance or composition (even though they brake the principles of OOP) or is there another design pattern? I personally think that using inheritance is going to be the lesser of the evils because of readability.

Its hard to say with everything being so abstract an ill-defined, but generally, you should use inheritance when it makes sense, and not when it doesn't, and composition when it makes sense and not when it doesn't.

If A and B truly have no relationship, then it probably makes more sense to structure things differently, with the stuff you want common in its own class that is used or inherited by A and B:

class hasXY {
    int X;
    int Y;
}

class A : hasXY { }
class B : hasXY { int Z }

or

class A {
    hasXY  XY;
}
class B {
    hasXY  XY;
    int    Z;
}

Of course, this introduces extra overhead in some languages (depending on their implementation details)

  1. Since Class B does not have a "has-a" relationship to class A .. so " Composition " should not be used.

  2. You can use OOP Inheritance way as you designed. It does not break OOP principles as you said. Class A & B are not 100% unrelated. They have common properties so OOP Inheritance way is acceptable. Remember that OOP does not require inheritance of Method members.

BUT for your case.You should duplicate Properties declaration so changes of a class does not affect the other.

class A
{
    public int X;
    public int Y;
}

class B
{
    public int X;
    public int Y;
    public int Z;
}

It all depends on what you mean by "they just share properties X and Y ".

Both properties are public, so they contribute to the behaviour of the class. Therefore, if the properties are really the same (have the same semantics), I would not say that the classes are unrelated. They share some behaviour, so it makes sense to use inheritance or better composition, which in most cases should be prefered.

But as you assume that A and B are unrelated, it is more likely that the properties are just unrelated variables of the same type to which you have assigned the same names. Therefore, just rename the properties in one of the classes and it will be clear that both the properties and the classes are unrelated.

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