Question

I know the theory but obviously when it comes to real world, I just do not know. In the following example is the relation between game and player/board a composition (they are instantionated inside the Game class)? And between a board and a pawn - aggregation, because the board can exist with 0 pawns?

Class Game
{
   Player p;
   Gameboard b;
  void Start()
  {
     p=new Player();
     b=new Gameborad();
  }
}

class Gameboard
{
   List<pawn> listOfpawns=new List<pawn>();
}
}
Was it helpful?

Solution

The difference between composition and aggregation is not whether a board can exist with zero pawns, but whether pawns can exist without boards, and also whether players and game boards can exist without a game.

In other words, if destroying a game will destroy all the players and boards then you have composition. If players and boards can live outside a game, then you have aggregation.

If destroying a board with destroy the pawns then you have composition. If pawns can live without a board, you have aggregation.

In general, if destroying the container destroys its elements, that's composition. Elements whose existence is not dependent on the container are said to be held by aggregation.

The classic examples of aggregation are:

  • Countries within alliances, because countries continue to exist if the alliance is disbanded
  • People within organizations, since the people still live when the organization ceases to exist

The classic examples of composition are:

  • The schedule(s) of a person, since if the person goes away, the schedule does too.

OTHER TIPS

Composition

An object contain other objects, stored by value. These are usually elements you think as internal to the object holding them, like a cardDeck object has card objects. Even if the deck isn't initially filled, the objects mostly have purpose only inside the holding object.

Aggregation

An object contains references to other objects, the aggregated objects are something that exist out in the program in another context, and the parent object is just holding them for organizational or functional reasons. Example given in the below site reference is a plane that holds person objects. The persons may come and go, and may be used in other parts of the program.

In This Case:

Your example I'd say falls more under Composition. Much like the deck of cards, the pawns have little meaning outside of the game board.

Terminology

The literalness "by value" or "by reference" depends on the language, when people say one or the other in this context they use it to refer to how strong an owning relationship the parent has.

Of course in Java all objects are stored by reference, But! As soon as no references to an object exist, it's eventually culled by the GC. The airplane is likely to be holding the only reference to the engine, so the engine dies when the airplane is removed. People can have other things referencing them beyond the airplane, so you can think of it less like the Airplane is holding them directly.

(Reference http://atomicobject.com/pages/Aggregation)

Ray and Vigilant are right but what is right for you depends more on the (goal of the) Application then it does on the objects itself.

  • Game/Player:
    • If the application is an ad-hoc Game: Player would be Composite
    • If the application does more with the Players (keeps scores/ranking): Player would be Aggregation

or

  • Airplane/Engine:
    • If the application is a Airline: Engine would be Composite
    • If the application is Maintenance: Engine would be Aggregation
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top