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.

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''', ''' Storage 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.


UML NOTATION

In UML , 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 code below shows what the source code is likely to look like.


// Composition
class Car
{
private:
  • itsCarb;

  • public:

virtual ~Car() {delete itsCarb;}
};



// Aggregation
class Pond
{
private:
  • > itsDucks;

  • };




COMPOSITE TYPES IN C

This is an example of composition in C .

typedef struct {
int age;
  • name;

  • enum { male, female } gender;

} 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 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 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 have references to others of the same type. In C, for example, a binary tree can be defined like:


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.


TIMELINE OF 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 Object s ( Class instances); languages in the ML family simply call them records. COBOL was the first Programming Language to support records directly; ALGOL 68 got it from COBOL and Pascal got it, more or less indirectly, from ALGOL 68.

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.

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;

1960 – ALGOL 60:

Arrays were the only composite data type in Algol 60 .

1968 – ALGOL 68:
int max=99;
mode newtypet = {Link without Title} {Link without Title} 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 {Link without Title} of integer;
b = record
a,b,c: real;
i,j,k: integer;
end;

1972 – K&R C :
#define max 99
struct newtypet {
double a, b, c; short i, j, k; float &r;
} newarrayt {Link without Title} {Link without Title} ;

1977: FORTRAN 77

Fortran 77 has arrays, but lacked any formal record/structure definitions. Typically compound structures were built up using EQUIVALENCE and/or COMMON statements:
  • 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;

1985 – C++:
const int max=99;
typedef class {
public: double a, b, c; short i, j, k; float &r;
} newtypet {Link without Title} {Link without Title} ;

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()
# now initialise an example array of this class #
newarrayt= [ newtypet() 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
  • 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

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 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 Professor;

class Department
{
...
private:
// Aggregation

};


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

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 s, Binary Tree s and Associative Array s.

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.

AGGREGATION IN COM



SEE ALSO