last edited byusericonadmin on 29-Apr-2010

Contents

A Simple Object

In component basics we introduced a component called a ThresholdCounter.cfc. Lets revisit our Threshold Counter now and take a look at it from an object oriented perspective:

<cfcomponent output="false">

    <cfset variables.threshold = 100>
    <cfset variables.currentTotal = 0>

    <cffunction name="addAmount" output="false" returntype="void">
        <cfargument name="amount" type="numeric" required="true">
        <cfset variables.currentTotal = variables.currentTotal + arguments.amount>
    </cffunction>

    <cffunction name="thresholdHasBeenReached" output="false" returntype="boolean">
        <cfset var thresholdReached = variables.currentTotal ge variables.threshold>
        <cfreturn thresholdReached>
    </cffunction>

</cfcomponent>

This component is used to create what we will call "Threshold Counter" objects, whose jobs are to add up numbers and to know if a particular maximum amount (our threshold) has been reached.

Using our simple object

Our object could be used like this:

<cfset counter = createObject("component","ThresholdCounter")>
<cfloop index="i" from="1" to="100">
    <cfset counter.addAmount(i)>
    <cfif counter.thresholdHasBeenReached()>
        <cfbreak>
    </cfif>
</cfloop>
<cfoutput>
    Threshold reached at value #i#.
</cfoutput>

So we just add sequential numbers and stop when the threshold has been reached.

This object demonstrate a couple of good OO concepts:

Let's take a quick look at each of these points

Contains both variables and functions

Typically objects will contain both variables AND functions packaged together.

In our case we have two variables:

And we have two functions:

Maintains state

In the example code above we create the object just once, then call the addAmount() and thresholdHasBeenReached() functions many times within the loop.

For each call to addAmount() the currentTotal variable is incremented, and the state of the object is changed.

Functions modify internal private data

Both of our variables are prefixed with "variables" (also known as "placed in the variables scope"). This means that they are "private" to the component and only functions inside the component can change them. For example, this code works fine:

<cfset counter = createObject("component","ThresholdCounter")>
<cfset counter.addAmount(20)>

But this code, will result in an error:

<cfset counter = createObject("component","ThresholdCounter")>
<cfset counter.currentTotal = counter.currentTotal + 1>

Our variable currentTotal is private and cannot be modified from outside the object.

The other point to note is that our threshold variable is also private to the object and the calling code has no knowledge of what that threshold value actually is. So this information is hidden privately inside our object.

Performs work

We have an object called a Threshold Counter. You can imagine that this object is a new friend of yours who is helping you to add up numbers and to let you know if a threshold has been reached.

You say to your new friend, "Add this number to your current count" and your friend does that job. You then ask, "Have we reached the threshold?" Your friend responds with a "yes" or "no".

So your friend, your object, is doing the work of adding up and checking if a threshold has been reached. You are not at all concerned with how your friend is keeping track of those numbers, you just trust that they are doing the job you asked them to do.

Improving Our Object with an Initialiser

Right now our object has a hard-coded value for the threshold level. It would be nice if we could set this value when the object is initially created. This change would allow us to create ThresholdCounters that have different thresholds.

In the next section we will discuss the init() function which allows us to prepare our objects before we start using them.

From here

Let's take a look at improving out object with the init() function.