Composition (computer science)
In
computer science,
composition is a way to combine objects where a structure might contain data objects, namely
- the "structure has a object that is a member of some class",
rather than the alternative
- "structure is a instance of some class".
In the
Unified Modeling Language has-a is depicted as a filled diamond. It always implies a multiplicity of 1, as only one object at a time can have lifetime responsibility for another object. When implementing in a language with
garbage collection the object can be used and created without worrying about memory leaks.
In the example below, C has no garbage collection, so the object must be freed by hand after the object has passed its useful life.
Example
This is an example of composition in C.
struct Person {
int age;
char name[16];
};
See also