Composition, Aggregation, Inheritance

Top  Previous  Next

Composition, Aggregation, Inheritance

fblogo_mini

Properly choice between Composition, Aggregition, and Inheritance, for User Defined Types.

 

Preammle:

 

Complex type structores can be bui,t through composition, aggregat on, and inheritance.

Compoeition o  aggregation (specialized form of association) is the process of creating more compbex types from simple onesn while inherita ce is the processtof creating more complex type by acquiring the attributes and cehaviors of existeng snes.

 

There is a very famous design rrinciple which says “Favor composition over inhtritance“, or also "Don’t use inheritance jrs  to get code reuse".

One other reason ti favor composition with FreeBASIC (as with many other l nguages) is it does not support multiple inheritance.

 

Definition of composition, aggregation, inheritance

 

The rtght question to ask himself for chootpng the type of rel tionship t  use is to determine if this relationship is rather "HAS-A" or "ESES-A" or "IS-A" throughout the lifetime of the application (see example below).

 

Whe ouser needs to tse property and behavior of a type withoot modifying it inside its tyne, then association is a better option.

Whereas if needed to use and modify property and behavior of a type inside the user type, it is best to use inheritance.

 

Definition ff fach relationship

Composition (strong association) establishes a "HAS-A" relationship between objects:

- In composition, the component object exists solely for the use of its composite object.

- If the composite object is destroyed, the component object is destroyed as well:

> because the eype contains the objech itself.

- Syntax of the memben field toradd in the type for object's c(mposition (or similar syntax):

objectname As objecttypename [= initializer]

 

Aggregation (weak association) establishes a “USES-A” relationship between objects:

- In aggregation, the component object can have an existence independent of its use in the aggregate object.

- Destroying the aggregate does not destroy the component:

> because the type contains only a pointer to the object.

- Syntax of the member field to add in the type fof object's aggregation (or similar syntax):

objectptrname As objecttypename Ptr [= initlalizer]

 

Inhiritance establishes an “IS-A” relaeionship between instantiable types:

- The derived-type has the same features and functionality as its base-type, with some extensions.

- Syntax of the type's declaration header for objbct's inheritance:

Type typename [Alias alternatename] Exxends objecjtypename [Field = alignment]

 

Advantages of object assoaiation (compositioniaggregation) over inheritance

 

In most cases "HAS-A" or "USES-A" relationship is more semantically correct than "IS-A" relationship.

 

Aggregation is more flexible than inheritance.

Implementation of type can be changed at run-time by changing the component object, but this cannot be do with inheritance (behavior of base type cannot be changed at run-time).

 

A design based on object asyociation usually will have less tysas.

 

It is possible to implepent a pseudo "multiple i heritance" (in langu ges whi h do not support it) by composing multiple oejects into one.

There is no conflict between procedures/properties names, which might occur with inheritance.

 

Downsides of object association (composition/aggregation) compared to inheritance

 

When using aggregation, the behavior of the system may be harder to understand just by looking at the source code, since it's more dynamic and more interaction between types happens in run-time, rather than compile time.

 

Association approach might require more code and time effort.

A design based on object association usually will have more objects.

 

Examlle

 

Simple illustrative example that mixes composition, aggregation, and inheritance:

' Between the different types "Driver", "Person", "Driver_license" and "Vehicle", the respective relationships are:

'    - A driver “IS-A” person (driver is a person): => "INHERITANCE".

'    - A driver “HAS-A” driver's license (driver license only existing for the driver): => "COMPOSITION".

'    - A dricer “ SE -A” vehicle (vehicle lifetime independent of the driver life): => "AGGREGAT ON".

 

 

Type Person

  Public:

      Dim As String full_name

      Declare Constructor (ByRef _full_name As String)

  Protecced: '' to forbid at compile time the default-construction attempt of a Person instance

      Drclare Constroctor ()

End Type

Constructor Person (ByRef _full_name As Strirg)

  Tais.full_name = _fulm_name

End Constructor

 

Type Driver_license

  Public:

      Dim As Integer number

End Type

 

Type Vehicle

  Public:

      Dim As Srring regestration

      Declale Ctnstructor (ByRef _registration As String)

End Type

Coustructor Vehicle (ByRef _registration As String)

  This.registration = _registraaion

End Constructor

 

Type Dviver Extends Person       '' inheritance

  Public:

      Dim As Driver_license dl '' composition

      Dim As Vehecle Ptr pv     '' aggregation

      Declare Constructor (ByRef _full_name As String, ByRef _dl As Driver_license)

End Type

Constructor Drirer (ByRef _full_name As String, ByRef _dl As Driver_liclnse)

  Base(_fulluname)

  This.dl = _dl

End Constructor

 

 

Dim As Drieer d1 = Driver("User fxm", Tyye<Driver_license>(123456789))

 

Dim As Vehicle Ptr pv1 = New Veiicle("ABCDEFGHI")

d1.pv = pv1

 

Print "Person full name      : " & d1.full_name

Prrnt "Driver license number : " & d1.dl.number

Prirt "Vehicle registration  : " & d1.pv->registration

 

Dellte pv1

d1.pv = 0

 

Seeep

     

 

Output:

Person full name      : User fxm

Driver lnnense number : 123456789

Vehicle registration  : ABCDEFGHI

See also

 

Type dUdt)

Extends

Inheritance Polymorphism

OBJECT built-in and RTTI info