0

Suppose a class Car which always creates the same object except that the Car objects can each one have a different motor inside them.

Is it composition because when a Car object is destroyed, its motor is also destroyed?

Or is it aggregation because the same type of motor could be inside different car objects?

I guess where I'm confused is in deciding whether some class is a composition or an aggregation. If you base your decision on whether a component is destroyed when its object is destroyed, what is a component? Is a component the concrete instance of a motor which was specifically created for a Car, or is it the motor class in general? In the first case, I want to say that it's composition, in the second, I want to say that it's aggregation.

I'm sorry I can't post code details, I'm not allowed to. I just want to have an abstract answer.

Thomas
  • 422
  • 1
  • 4
  • 12
  • Does this answer your question? [What is the difference between association, aggregation and composition?](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) – jaco0646 Jun 19 '20 at 22:09
  • @jaco0646 In part, yes. But I wanted a more in-depth explanation of how objects in software and objects in real world compare. – Thomas Jun 20 '20 at 09:26

1 Answers1

1

I think about it like this:

  • existence dependency (existence of something depending on the existence of another thing)
  • using the word "uses" for aggregation and "owns" for composition

Example of aggregation:

  • A music band uses people. If you destroy the band it doesn't necessarily "destroy" the people. Meaning that the existence of the people do not depend on the existence of the band, as you can create another band with the same people.

Example of composition:

  • A company owns accounts. If you destroy the company, the accounts will be destroyed as well. Meaning that the existence of the accounts depend on the existence of the company, as you cannot have an account of a company that does not exist and you cannot use those accounts in another company.

So a Car and Motor relationship it can be both and it all depends on how you want to design your code, but I would think of it as an aggregation as you can use the same Motor instance in two different Cars or you can change the Motor of the Car.

When designing your code, think of where is the best place to create the instance of Motor:

  • If you create the instance of Motor inside the class Car, the existence of the Motor instance will depend on the existence of the Car instance it was created in. (Composition)

In Python:

class Motor:
   ...

class Car:
def __init__(self):
   self.motor = Motor()

my_car = Car()
  • If you create the instance of Motor outside the class Car, the existence of the Motor instance will not depend on the existence of the Car instance you passed it to. (Aggregation)

In Python:

class Motor:
   ...

class Car:
def __init__(self, motor):
   self.motor = motor

fast_motor = Motor()
my_car = Car(fast_motor)
Julio Suriano
  • 141
  • 1
  • 1
  • 7
  • 1
    "You can use the same Motor instance in two different Cars": this is exactly the nuance I'm trying to understand. You say that a motor instance can be used in two different cars, but in the real world it isn't possible to have the same motor object in two cars at the same time, right? So when we think of composition or aggregation, we mustn't really think about objects as they would be in the real-world, but rather as it makes sense in our code? – Thomas Jun 20 '20 at 09:20
  • Hmm now you got me thinking haha. In the real-world we could transfer a Motor to another Car, but we cannot have the same Motor in the two different cars at the same time. It is a very tricky relationship as a composition and an aggregation both will be valid, so I think it will all depend on the application you are designing and basically think of the Motor's existence, will it depend on the Car or not? – Julio Suriano Jun 20 '20 at 14:57