Object-Oriented Blog Process - IV

Classes and intstances

In my last post I coined the expressions class and instrance. I suspect that many of you reading this, even though you might be OO newbees like me, are familiar with these terms, and what they mean. Never the less, I'm going to have a go at explaining them in my own words. Mostly because a blog series like this would be incomplete without it, but also to root my own understanding of it.

Let's go back to the example from last time. We have a webshop, and as so it will be filled with products. So we'll have a bunch of product objects. The product class will be what deceides what attributes and what methods a product object will have.

It's kind of like writing down on a piece of paper a form with fields for product attributes. To make it easy for our self we keep the original paper clean - and each time we need another product in our catalouge we hit the "Copy" button on our copy machine. It's the same in our code. We write a class file, and for each new product we'll programatically trigger a "copy button". The cool thing in programing versus the paper version is that when you do changes to the class/original file (like adding an atribute) - these changes are reflected in all objects made from that class.

The product object we create from the product class are what we call instances. A product object is an instance of the product class.

In ColdFusion a class file would be a .cfc, but before starting to code it's usual to plan the application (kinda like an architects drawings). The way a class is planed/drawed will most often be like this:

(For planing OO application I've earlier bloged about a marvelous tool for doing this)

Here we see the class name, the attributes and the methods. So based on our plan we can now code our cfc - something like this:

<cfcomponent displayname="Product" output="false">

 <!--- PROPERTIES --->
 <cfset variables.instance = StructNew() />

 <!--- INITIALIZATION / CONFIGURATION --->
 <cffunction name="init" access="public" returntype="product" output="false">
  <cfargument name="productName" type="string" required="false" default="" />
  <cfargument name="productPrice" type="numeric" required="false" default="" />
  <cfscript>
   setProductName(arguments.productName);
   setProductPrice(arguments.productPrice);
   return this;
  </cfscript>
  </cffunction>

 <!--- METHODS --->
 
 <cffunction name="setProductName" access="public" returntype="void" output="false">
  <cfargument name="productName" type="string" required="true" />
  <cfset variables.instance.productName = arguments.productName />
 </cffunction>
 
 <cffunction name="getProductName" access="public" returntype="string" output="false">
  <cfreturn variables.instance.productName />
 </cffunction>

<cffunction name="setProductPrice" access="public" returntype="void" output="false">
  <cfargument name="productPrice" type="double" required="true" />
  <cfset variables.instance.productPrice = arguments.productPrice />
 </cffunction>
 
 <cffunction name="getProductPrice" access="public" returntype="double" output="false">
  <cfreturn variables.instance.productPrice />
 </cffunction>

</cfcomponent>

(To quickly create this code I used the Rooibos Generator by Peter J. Farrell)

Then to create instances of the product class, or product objects we do this:

<cfset golfBall = createObject("component","product").init("Boyzoid Custom High-Speed","12.95") />
<cfset golfShorts = createObject("component","product").init("Frank Shortser","9.50") />

Voila - we now have two instances, or two product objects, a golf ball and a golf shorts.

I hope that you now understand the terms class and instance. If you don't quite understand what's going on in the code above, I'll explain that further in my next post.

- - -

As before the examples and code displayed here are simplified, and just there to illustrate the consepts that I talk about. They do not show the full or correct implementation for a real world solution.

Comments