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 StableRoom
s and Horse
s.
You perhaps ought to make a StableRoom
as its own class/entity.
Now you can have composition of StableRoom
s to the Stable
, and aggregation of Horse
s to StableRoom
s.
A StableRoom
makes sense to be destroyed when the Stable
is destroyed, but when destroyed the Horse
s assigned to the StableRoom
s 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.