Object Composition Article Index for
Object
Website Links For
Object
 

Information About

Object Composition




Composition is contrasted with Subtyping , which is the process of adding detail to a general data type to create a more specific data type. In composition, the composite type " Has An " object of a simpler type, while in subtyping, the subtype " Is An " instance of its parent type. Composition does not form a subtype but a new type.

Composited objects are called fields, '''items''', '''members''' or '''attributes''', and the resulting composition a '''structure''', '''record''', '''tuple''', or ''' Composite Type '''. The terms usually vary across languages. Fields are given a unique name so that each one can be distinguished from the others. Sometimes an issue of ownership arises: when a composition is destroyed, should objects belonging to it be destroyed as well? If not, the case is sometimes called '''aggregation'''. For more, see the aggregation section below.

In UML , composition is depicted as a filled diamond. It always implies a multiplicity of 1 or 0..1, as no more than one object at a time can have lifetime responsibility for another object. The more general form of composition, that of aggregation, is depicted as an unfilled diamond.


EXAMPLE

This is an example of composition in C .

typedef struct {
int age;
  • name;

  • enum { male, female } sex;

} Person;


  • , and enum {male, female} are combined to form the composite type of Person. Each object of type Person then "has an" age, name, and sex.


If a Person type were instead created by subtyping, it might be a subtype of Object, and it could inherit some attributes from Object (every object has an age), while extending the definition of Object with new attributes (not every object has a sex, but every person does).


RECURSIVE COMPOSITION

Objects can be composited recursively with the use of Recursive Type s or Reference s. Consider a tree. Each node in a tree may be a branch or leaf; in other words, each node is a tree at the same time when it belongs to another tree.

One implementation for the recursive composition is to let each object to have references to others of the same type. In C, for example, a binary tree can be defined like:


typedef struct bintree {
  • left, ---right;

  • } tBinaryTree;



If pointers left and right are valid, the node is thought to be a branch referring to each tree to which left and right point. If not, the node is a leaf. In this way, the recursion can be terminated.

Another is to use a tagged union. See Tagged Union for an example.


COMPOSITION IN VARIOUS LANGUAGES

C calls a record a Struct or structure; Object-oriented languages such as Java , Smalltalk , and C++ often keep their records hidden inside objects (class instances); languages in the ML family simply call them records. COBOL was the first Programming Language to support records directly; Algol got it from COBOL, and Pascal got it, more or less indirectly, from Algol.

For more details about composition in C/C++, see Composite Type .


AGGREGATION

Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a University owns various departments (e.g., Chemistry ), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, A Professor could work in more than one department, but a department could not be part of more than one university.

Composition is usually implemented such that an object contains another object. For example, in C++ :


class Department;

class University
{
...
private:
Department faculty {Link without Title} ;
...
};


In aggregation, the object may only contain a reference or pointer to the object:


class Professor;

class Department
{
...
private:

};


Sometimes aggregation is referred to as composition when the distinction between ordinary composition and aggregation is unimportant.


UML

In UML , composition is depicted as a filled diamond and a solid line. Aggregation is depicted as an open diamond and a solid line. The below image shows compostion, and then aggregation. The code below shows what the source code is likely to look like.


class Car
{
public:
virtual ~Car() {delete itsCarb;}
private:
  • itsCarb

  • };




class Node
{
private:
  • > itsNodes;

  • };




SEE ALSO