Object composition
Encyclopedia
In computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

, object composition (not to be confused with function composition
Function composition (computer science)
In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones...

) is a way to combine simple objects
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 or data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

s into more complex ones. Compositions are a critical building block of many basic data structures, including the tagged union
Tagged union
In computer science, a tagged union, also called a variant, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly...

, the linked list
Linked list
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference to the next node in the sequence; more complex variants add additional links...

, and the binary tree
Binary tree
In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to...

, as well as the object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 used in object-oriented programming.

Composited (composed) objects are often referred to as having a "has a
Has-a
In database design and object oriented program architecture, has-a is a relationship where one object "belongs" to another object , and behaves according to the rules of ownership. In simple words, has-a relationship in an object is called a member field of an object...

" relationship. A real-world example of composition may be seen in an automobile: the objects wheel
Wheel
A wheel is a device that allows heavy objects to be moved easily through rotating on an axle through its center, facilitating movement or transportation while supporting a load, or performing labor in machines. Common examples found in transport applications. A wheel, together with an axle,...

, steering wheel
Steering wheel
A steering wheel is a type of steering control in vehicles and vessels ....

, seat, gearbox and engine
Engine
An engine or motor is a machine designed to convert energy into useful mechanical motion. Heat engines, including internal combustion engines and external combustion engines burn a fuel to create heat which is then used to create motion...

may have no functionality by themselves, but an object called automobile
Automobile
An automobile, autocar, motor car or car is a wheeled motor vehicle used for transporting passengers, which also carries its own engine or motor...

containing all of those objects would serve a higher function, greater than the sum of its parts.

When, in a language, objects are typed, types can often be divided into composite and noncomposite types, and composition can be regarded as a relationship between types: an object of a composite type (e.g. car) "has an
Has-a
In database design and object oriented program architecture, has-a is a relationship where one object "belongs" to another object , and behaves according to the rules of ownership. In simple words, has-a relationship in an object is called a member field of an object...

" object of a simpler type (e.g. wheel).

Composition must be distinguished from subtyping, which is the process of adding detail to a general data type to create a more specific data type. For instance, cars may be a specific type of vehicle: car is a
Is-a
In knowledge representation, object-oriented programming and design, is-a or is_a or is a is a relationship where one class D is a subclass of another class B ....

 vehicle. Subtyping doesn't describe a relationship between different objects, but instead, says that objects of a type are simultaneously objects of another type.

In programming languages, composite objects are usually expressed by means of references from one object to another; depending on the language, such references may be known as fields, members, properties or attributes, and the resulting composition as a structure, storage record
Storage record
In computer science, a storage record is:* A group of related data, words, or fields treated as a meaningful unit; for instance, a Name, Address, and Telephone Number can be a "Personal Record"....

, tuple
Tuple
In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an n-tuple is a sequence of n elements, where n is a positive integer. There is also one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair...

, user-defined type (UDT), or composite type
Composite type
In computer science, a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types...

. Fields are given a unique name so that each one can be distinguished from the others. However, having such references doesn't necessarily mean that an object is a composite. It is only called composite if the objects it refers to are really its parts, i.e. have no independent existence. For details, see the aggregation section below.

UML notation

In UML
Unified Modeling Language
Unified Modeling Language is a standardized general-purpose modeling language in the field of object-oriented software engineering. The standard is managed, and was created, by the Object Management Group...

, composition is depicted as a filled diamond and a solid line.
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, aggregation, is depicted as an unfilled diamond and a solid line. The image below shows both composition and aggregation. The C++ code below shows what the source code is likely to look like.



// Composition
class Automobile
{
private:
Carburetor* carb;
public:
Automobile : carb(new Carburetor) { }
virtual ~Automobile { delete carb; }
};



// Aggregation
class Pond
{
private:
std::vector ducks;
};

Composite types in C

This is an example of composition in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

.

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 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 gender, but every person does).

Recursive composition

Objects can be composited recursively with the use of recursive type
Recursive type
In computer programming languages, a recursive data type is a data type for values that may contain other values of the same type...

s or references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

. 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 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
Tagged union
In computer science, a tagged union, also called a variant, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly...

 for an example.

Timeline of composition in various languages

C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 calls a record a struct
Struct (C programming language)
A struct in C programming language is a structured type that aggregates a fixed set of labelled objects, possibly of different types, into a single object.A struct declaration consists of a list of fields, each of which can have any type...

 or structure; object-oriented languages such as Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

, Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

, and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 often keep their records hidden inside objects
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 (class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 instances); languages in the ML
ML programming language
ML is a general-purpose functional programming language developed by Robin Milner and others in the early 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM...

 family simply call them records. COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

 was the first programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 to support records directly; ALGOL 68
ALGOL 68
ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...

 got it from COBOL and Pascal got it, more or less indirectly, from ALGOL 68. Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

 provides structures and classes (the latter via the Common Lisp Object System).

1959 – COBOL

01 customer-record.
03 customer-number pic 9(8) comp.
03 customer-name.
05 given-names pic x(15).
05 initial-2 pic x.
05 surname pic x(15).
03 customer-address.
05 street.
07 house-number pic 999 comp.
07 street-name pic x(15).
05 city pic x(10).
05 country-code pic x(3).
05 postcode pic x(8).
03 amount-owing pic 9(8) comp.


1960 – ALGOL 60

Arrays were the only composite data type in Algol 60
ALGOL 60
ALGOL 60 is a member of the ALGOL family of computer programming languages. It gave rise to many other programming languages, including BCPL, B, Pascal, Simula, C, and many others. ALGOL 58 introduced code blocks and the begin and end pairs for delimiting them...

.

1964 – PL/I

dcl 1 newtypet based (P);
2 (a, b, c) fixed bin(31),
2 (i, j, k) float,
2 r ptr;
allocate newtypet;


1968 – ALGOL 68

int max = 99;
mode newtypet = [0..9] [0..max]struct (
long real a, b, c, short int i, j, k, ref real r
);
newtypet newarrayt = (1, 2, 3, 4, 5, 6, heap real := 7)


For an example of all this, here is the traditional linked list declaration:

mode node = union (real, int, compl, string),
list = struct (node val, ref list next);


Note that for ALGOL 68 only the newtypet name appears to the left of the equality, and most notably the construction is made – and can be read – from left to right without regard to priorities.

1970 – Pascal

type
a = array [1..10] of integer;
b = record
a, b, c: real;
i, j, k: integer;
end;


1972 – K&R C
  1. define max 99

struct newtypet {
double a, b, c;
float r;
short i, j, k;
} newarrayt[10] [max + 1];


1977 – FORTRAN 77
Fortran 77 has arrays, but lacked any formal record/structure definitions. Typically compound structures were built up using EQUIVALENCE or COMMON statements:

CHARACTER NAME*32, ADDR*32, PHONE*16
REAL OWING
COMMON /CUST/NAME, ADDR, PHONE, OWING


1983 – ADA

type Cust is
record
Name : Name_Type;
Addr : Addr_Type;
Phone : Phone_Type;
Owing : Integer range 1..999999;
end record;


1983 – C++

const int max = 99;
class{
public:
double a, b, c;
float &r;
short i, j, k;
}newtypet[10] [max + 1];


1991 – Python

max = 99
class NewTypeT:
def __init__(self):
self.a = self.b = self.c = 0
self.i = self.j = self.k = 0.0
class R:
def __init__(self):
self.r = None
self.r = R
  1. Initialise an example array of this class.

newarrayt = ewTypeT for i in range(max + 1)] for j in range(10)]


1992 – FORTRAN 90
Arrays and strings were inherited from FORTRAN 77, and a new reserved word was introduced: type


type newtypet
double precision a, b, c
integer*2 i, j, k
  • No pointer type REF REAL R

end type

type (newtypet) t(10, 100)


FORTRAN 90 updated and included FORTRAN IV's concept called NAMELIST.

INTEGER :: jan = 1, feb = 2, mar = 3, apr = 4
NAMELIST / week / jan, feb, mar, apr


1994 – ANSI Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...


Common Lisp provides structures and the ANSI Common Lisp standard added CLOS classes.


(defclass some-class
((f :type float)
(i :type integer)
(a :type (array integer (10)))))


For more details about composition in C/C++, see Composite type
Composite type
In computer science, a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types...

.

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
University
A university is an institution of higher education and research, which grants academic degrees in a variety of subjects. A university is an organisation that provides both undergraduate education and postgraduate education...

 owns various departments (e.g., chemistry
Chemistry
Chemistry is the science of matter, especially its chemical reactions, but also its composition, structure and properties. Chemistry is concerned with atoms and their interactions with other atoms, and particularly with the properties of chemical bonds....

), 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++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

:


class Professor;

class Department
{
...
private:
// Aggregation
Professor* members[5];
...
};

class University
{
...
private:

Department faculty[20];
...

create dept
{
...
// Composition
faculty[0] = Department(...);
faculty[1] = Department(...);
...
}
};

In aggregation, the object may only contain a reference or pointer to the object (and not have lifetime responsibility for it):

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:

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 list
Linked list
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference to the next node in the sequence; more complex variants add additional links...

s, binary tree
Binary tree
In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to...

s and associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....

s.

In UML
Unified Modeling Language
Unified Modeling Language is a standardized general-purpose modeling language in the field of object-oriented software engineering. The standard is managed, and was created, by the Object Management Group...

, 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 number of instances of the composited data type.

Aggregation in COM

In Microsoft's Component Object Model
Component Object Model
Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...

 aggregation means that an object exports, as if it were their owner, one or several interfaces
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

 of another object it owns. Formally, this is more similar to composition or encapsulation than aggregation. However, instead of implementing the exported interfaces by calling the interfaces of the owned object, the interfaces of the owned object themselves are exported. The owned object is responsible for assuring that methods of those interfaces inherited from IUnknown
IUnknown
In programming, the IUnknown interface is the fundamental interface in the Component Object Model . The published mandates that COM objects must minimally implement this interface...

 actually invoke the corresponding methods of the owner. This is to guarantee that the reference count of the owner is correct and all interfaces of the owner are accessible through the exported interface, while no other (private) interfaces of the owned object are accessible.

See also

  • C++ structure
    C++ structure
    The C++ programming language allows programmers to separate program-specific datatypes through the use of classes. Instances of these datatypes are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer...

  • Composite type
    Composite type
    In computer science, a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types...

  • Law of Demeter
    Law of Demeter
    The Law of Demeter or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling...

  • Implementation inheritance
  • Inheritance semantics
  • Virtual inheritance
    Virtual inheritance
    Virtual inheritance is a topic of object-oriented programming. It is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes common direct base for the derived class and any next class that derives from it...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK