In computer science, composition is a way and practice to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures, including the tagged union, the linked list, and the binary tree, as well as the object used in object-oriented programming.
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.
typedef struct {
int age;
char *name;
enum { male, female } sex;
} Person;
In this example, the primitive types int, char *, 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).
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 {
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.
For more details about composition in C/C++, see Composite type.
Composition is usually implemented such that an object contains another object. For example, in C++:
class Department;class University { ... private: Department faculty*; ... };
In aggregation, the object may only contain a reference or pointer to the object:
class Professor;class Department { ... private: Professor* members*; ... };
Sometimes aggregation is referred to as composition when the distinction between ordinary composition and aggregation is unimportant.
class Car
{
public:
virtual ~Car() {delete itsCarb;}
private:
Carburetor* itsCarb
};
class Pond
{
private:
vector itsDucks;
};
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Object composition".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world