In the following diagram I have a Car which has a Motor.

An Audi A6 is a Car which has a Motor of type 2.4 L V6.

So both the Car and the Audi have a composition on a motor. However it looks incorrect to have two composition on this diagrams.

What is the correct way?

enter image description here


Stop using inheritance, it is the wrong tool for this job.

Instead, favor compositional typing.

class Car
     String VIN;
     CarModel ModelType;
     Motor Motor;

class CarModel
    String Name;
    MotorModel DefaultEngineType;

class MotorModel
    Strimg Name;
    int Cylinders;
    int Displacement;

class Motor
    MotorModel MotorType;
    int WearAndTear;

A specific car has an engine, a carmodel has a default engine that is associated with it. Inheritance has a hard time modeling this.

Using compositional typing, it is possible to change out the engine of an Audi, and have it use a Chevy big block if you are so inclined :D

A CarModel doesn't have an engine, just a default specification for what type of engine should be placed in that kind of car.

  • 1
    Your classes CarModel and Motor have a MotorModel. Should the EngineModel class be changed to MotorModel? – DFord May 17 '18 at 14:27
  • @DFord good catch, edited :) – TheCatWhisperer May 17 '18 at 14:45
  • 1
    In this case I totally agree with you. I should have found another example. My question is more on the composition thing rather than the model. – nowox May 17 '18 at 16:09
  • @nowox please be more specific – TheCatWhisperer May 17 '18 at 16:24
  • 2
    After a second thought in my use case your solution is the right solution. – nowox May 17 '18 at 18:31

The orange connection should not be there, it is meaningless. The blue one already says it all at the right level. Note that Car and Audi A6 are not really different entities, Car just represents the shared part of all car types. And that shared part has the relationship with the motor.

  • Without the orange connection I allow my Audi to have any kind of engine. Am I wrong? – nowox May 17 '18 at 19:40
  • Well, yes, but with the orange connection that would still be the case because the generic car-motor relationship remains. With both connections (blue and orange) your Audi A6 would have two engines: any engine plus a 2.4 L V6 engine. The point of your model is not to carve any possible combination of components in stone, it is supposed to provide some relational type logic. A car has one engine and you may have different car types. You may want to apply a constraint to the engine type but you do not want to include any possible object in your model. It is a class collaboration diagram. – Martin Maat May 18 '18 at 7:34

I would think that accurate UML depictions will depend entirely upon usage. For example, does the Audi A6 model directly instantiate a 2.4 L V6 instance? Alternatively, does a client builder perform some type of dependency injection? Both of those would create different answers to your question.

In the event of directly creating the object, I would use a dependency link since you must include the definition of that engine type. Adding stereotypes (refine, instantiate, etc) should theoretically clarify your intent.

When using dependency injection, I would add the client injector so that the diagram can look more like it does here: https://en.wikipedia.org/wiki/Dependency_injection

  • @nowox I would argue that the accepted answer within this question is not actually answering the question as much as suggesting to not ask it. TheCatWhisperer's answer is useful and should be upvoted because it is good advice. It also happened to match the use case that the OP desired which is bonus! However, that is the difference between upvotes and accepted answers. I honestly do not believe it answers the intent of the question, which is how to get the correct result out of UML as asked. NOTE: I do not yet have rep to reply to his comment, otherwise I would have. – Gren Meera May 17 '18 at 19:34
  • I agree with you. In your answer, yes the Audi A6 directly instantiate this kind of engine. – nowox May 17 '18 at 19:42
  • In that case, removal of the composition connection and replacing it with a stereotyped dependency would seem to be appropriate UML. – Gren Meera May 17 '18 at 19:53
  • What do you mean by a stereotyped dependency? – nowox May 17 '18 at 19:55
  • A dependency is the dotted arrow relationship type. Adding stereotypes allow descriptions to better refine how the dependency is working. Here is an example. – Gren Meera May 17 '18 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.