Composition

The next basic concept of object-oriented programming to introduce is that of composition.

Composition is the concept that an object cannot exist without its composite parts. In essence, it is the process of creating an object from a combination of other objects.

A common way to show this example is in the form of a car. Many objects go into making a car. Some of them are 'optional extras' and trimmings from the dealership such as a cup holder or satellite navigation system, whereas others are crucial for the existence of the car, such as an engine or wheels.

The following UML diagram provides another helpful visualization of composition:

Composition

Composition is represented in UML as a filled diamond shape and a solid black line.

An engine on its own is just an engine. In true object-oriented practices, as an object it cannot exist without the car such as its parent object. Similarly, a car is an object, but is nothing without an engine.

On their own, these objects are not of much use. Combining them together, the individual components compose an object that is incredibly useful and useable.

The "HAS A" Relationship

One method of confirming you have true composition is again checking the relationship between the components.

Simply referred to as a "HAS A" relationship, in this example a car "HAS A" engine. An engine does not have a car.

The following code contains the entire Car object component. Create a new file called Car.cfc and save it to the following directory: com/packtApp/oop/beans. Insert the code from Listing 4.6 into Car.cfc.

<cfcomponent displayname="Car"
output="false"
hint="I am the Car Class.">
<cfproperty name="engine" type="any" default="" />
<cfset variables.instance = structNew() />
<cffunction name="init" access="public"
output="false" returntype="any"
hint="I am the constructor method
for the Car Class.">
<cfset variables.instance.engine =
createObject('component', 'engine') />
<cfreturn this />
</cffunction>
<!--- getters / accessors --->
<cffunction name="getEngine" access="public"
output="false" hint="I return the engine.">
<cfreturn variables.instance.engine />
</cffunction>
</cfcomponent>

Listing 4.6 - A Car object, which requires an engine

You can see from the code in Listing 4.6 that the Car object instantiates and sets the Engine object itself within the init() constructor method. This highlights the fact that the Car cannot exist without its composite objects, in this simple example the Engine.

Create a new file called Engine.cfc and save it within the following location: com/packtApp/oop/beans. Copy the code from Listing 4.7 into Engine.cfc to create the composite component required by our Car object.

<cfcomponent displayname="Engine"
output="false"
hint="I am the Engine Class.">
<cfset variables.instance = structNew() />
<cffunction name="init" access="public"
output="false" returntype="any"
hint="I am the constructor method
for the Engine Class.">
<cfreturn this />
</cffunction>
</cfcomponent>

Listing 4.7 - An Engine object, required for the car to exist

In this example, the Engine object itself is incredibly simple. Most likely within a real-world application, any composite objects would include more methods and functions than purely a constructor method.

Let's instantiate our Car object to see the composition in action.

The following code in Listing 4.8 contains the createObject() method to instantiate the Car object, followed by a cfdump to view the object itself.

<cfset objCar =
createObject('component',
'com.packtApp.oop.beans.Car')
.init() />
<cfdump var="#objCar#" />

Listing 4.8 - Instantiating the Car object

Our Car object requires no arguments or parameters, and so we do not need to send any values into the constructor method. We understand that the Car object will create its required Engine object and set the Engine into its variables.instance structure.

The "HAS A" Relationship

The output from the cfdump tag in Listing 4.8 can be seen in the previous image, in which you can clearly see the Engine object nested within the properties of our Car object, created as a requirement for our Car to exist.

Implied ownership

The concept of composition implies a certain level of ownership. When our Car object is destroyed, so are the contained objects and its composite parts. No more engine, no more wheels, no more pine-scented air freshener.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.31.67