1

I am trying to decide the type of relationship (or association) there is between the following two classes:

Class Stable
{
private const int MaxStableRooms;
private Horses[] Horses;

public Stable(int MaxStableRooms)
{
Horses = new Horse[MaxStableRooms];
for  (int i = 0; i < horsecount; i++)
  Horses[i]  = new Horse();
}

}

Class Horse:Animal
{
 ...
}

(I hope I have provided sufficient detail in the above example)

I have a feeling it is Composition. If the Stable is destroyed, so the horses too.

Is this correct?

3
  • 2
    What else do you think it could be? I think you may be asking if it is aggregation or composition, is that correct? Commented Sep 27, 2016 at 14:46
  • Composition in the first instance...but not sure if it fits with aggregation? Commented Sep 27, 2016 at 14:46
  • The main question is: "Can horses exist without a stable?". The classic definition says: If no, it's composition, if yes, it's aggregation. You seem to be unclear about this, because you are creating the stable with horses, and the assumption would be the horses are destroyed when the stable is (which doesn't quite fit with reality)
    – tofro
    Commented Sep 27, 2016 at 14:59

3 Answers 3

2

To add to @RobertHarvey's good answer and @tofro's excellent observation in comment:

If the Stable is destroyed, so the horses too.

This snippet is unrealistic in a number of ways:

Only because the Horses array is shown as being allocated once, marked private, and the example is otherwise incomplete can we imagine that maybe the Horses are lost by destroying the Stable. However, for example, all it would take to not lose horses is for them to be exposed outside of the Stable class, and captured; it is plausible that another class catalogues all the horses, for example.

It is odd that the Stable creates new horses in its constructor, but just because it does doesn't necessarily mean that it destroys them at its destruction.

That it creates new horses means that creating new Stable immediately represents a fully populated Stable not necessarily capable of holding existing horses. Where can I put an existing horse? Not in such a Stable, they're always full of brand new horses.

More realistic would be:

That the Stable is pre-configured (i.e. created) with a fixed number of stalls, each of which can dynamically be either empty or assigned to a horse.

And that over time the same Stable could be used to assign any, if available, (or a particular) empty stall to a horse, or remove an assignment of a horse to return a stall to empty assignment.

Horse (object) creation would be separated from Stable (object) creation.


However, what you are alluding to with the notion of the count of MaxStableRooms, but not properly modeling, is the distinction between StableRooms and Horses.

You perhaps ought to make a StableRoom as its own class/entity.

Now you can have composition of StableRooms to the Stable, and aggregation of Horses to StableRooms.

A StableRoom makes sense to be destroyed when the Stable is destroyed, but when destroyed the Horses assigned to the StableRooms are made homeless but not destroyed.

By making a first-class notion of StableRoom, each StableRoom has the potential for having differentiated qualities, such as max horses, adjoining doors etc...

(In an analogous case with HotelRooms, you'd have number of beds, whirlpool bath, ocean view vs. pool view, rate category, etc..)


Further, if you needed more complex assignments (as would be necessary for hotels, hotel rooms, and people) you might have another class (or set of classes) involved in the scheduling & calendaring and bookings rather than doing that entirely within the Stable class.

1
  • Oops, I reverse the terms composition and aggregation in my text above. Sorry. Now fixed.
    – Erik Eidt
    Commented Sep 27, 2016 at 20:32
3

Check your is-a and has-a relationships. "is-a" is inheritance. "has-a" is composition.

So Horse "is a" animal is inheritance. Stable "has a" horse (or horses) is composition.

1
  • 2
    It that why I'm having so much trouble riding my stable? Commented Sep 27, 2016 at 17:59
1

Your code sample is a case of containment.

Composition is a fuzzy term that can mean just about anything. I suggest to not use it. The two counter terms you want to understand are aggregation versus containment.

With containment, the inner object is fully encapsulated by the outer object, hence not directly accessible from outside the outer object. You have to ask the outer object nicely and hope it will pass on the message to the inner object.

With aggregation, the outer object publishes the interface of the inner object (its reference) so the inner object can be accessed beyond the outer object's control.

Typically, with containment the inner object is a member of the outer object where it is not with aggregation but this is not necessarily the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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