Object composition
From Wikipedia, the free encyclopedia
In computer science, object composition (not to be confused with function 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.
A real-world example of composition may be seen in an automobile: the objects wheel, steering wheel, seat, gearbox and engine may have no functionality by themselves, but if you compose them, they may form an automobile object, which has a higher function, greater than the sum of its parts in a trite sense.
Composited (composed) objects are called fields, items, members or attributes, and the resulting composition a structure, record, tuple, user-defined type (UDT), 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.
Contents |
[edit] Example
This is an example of composition in C.
typedef struct {
int age;
char *name;
enum { male, female } gender;
} 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 Organism
, and it could inherit some attributes from Organism
(every organism has an age), while extending the definition of Organism
with new attributes (not every organism has a sex, but every person does).
[edit] Recursive composition
Objects can be composited recursively with the use of recursive types or references. 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:
struct bintree {
struct bintree *left, *right;
// some data
};
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.
[edit] 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.
[edit] 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 continue to exist. 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[20]; ... };
In aggregation, the object may only contain a reference or pointer to the object (and have not lifetime responsibility for it):
class Professor; class Department { ... private: Professor* members[5]; ... };
Sometimes aggregation is referred to as composition when the distinction between ordinary composition and aggregation is unimportant.
The above code would transform into the following UML Class diagram:
[edit] Containment
Composition that is used to store several instances of the composited data type is referred to as containment. Examples of such containers are arrays, linked lists, binary trees and associative arrays.
In UML, containment is depicted with a multiplicity of 1 or 0..n (depending on the issue of ownership), indicating that the data type is composed of an unknown amount of instances of the composited data type.
[edit] 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 compositions, and then aggregation. The code below shows what the source code is likely to look like.
class Car { public: virtual ~Car() {delete itsCarb;} private: Carburetor* itsCarb; };
class Pond { private: vector<Duck*> itsDucks; };