YOUR FEEDBACK
José D'Andrade wrote: "...it may never be released..." Why? "...if Midori isn’t heir to Windows Mi...
AJAXWorld RIA Conference
$300 Savings Expire August 8
Register Today and SAVE!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Baking Object-Oriented Pizza with ColdFusion
Procedural programming and OO programming buzzwords

So, Let's Put Some CFCs into the Mix
This whole example is about pizza. Most of the functions we're doing are being done on the pizza. So, we can create a Pizza CFC to handle a lot of this functionality. A few methods to put in the CFC are Bake, Prepare, Cut, and Deliver. Instance variables in the CFC might be price, ingredients, and the order information. Instead of executing multiple custom tags and passing data back and forth, we pass the data into the pizza component, and then merely call the component's methods (or functions) to act on the data. A sample of this approach can be seen in Listing 1. As with the custom tags, I didn't flesh out the code of the CFC.

Is the CFC in this example an object? Technically it meets the definition of an object from the OO paradigm. It also meets the definition of an Abstract Data Type (ADTs) from the procedural paradigm. For all intents and purposes objects and ADTs are the same. They are containers for data that also abstract functionality to perform actions on the data. Today everyone calls them objects because that's a buzzier word.

Add Some Object Orientation
Object-oriented programming is supposed to build a model that mimics the way things really operate in the real world. In procedural programming, your model is built to make it easy for the computer to process. I once heard it said that procedural is requesting the data and doing things to it. Using this as a basis, Listing 2 is very procedural. It's not mimicking the real world; it's breaking things up into small chunks to send to the computer processor for processing.

A pizza doesn't know how to deliver itself to the customer. It doesn't know how to prepare itself. It does not know how to bake itself, or cut itself into slices. In an OO model, this functionality wouldn't be put on a pizza object. An OO is geared towards telling your "things" how to act on the data they already have. Next we'll modify the example to be a bit more OO.

The pizza doesn't actually do anything in the real world. The robots or customers do the real tasks. If we were to create an object for it, it would just be a data container to pass around to objects. Here are a few of the actions that occur in the system:

  • OrderPizza: This would be something that the customer does.
  • PreparePizza: This is something that the cook does.
  • BakePizza: This is also something done by the cook.
  • Cut Pizza: The cook cuts the pizza.
  • Receive Order: When the customer orders the pizza, the waitress gets the order. When the waitress gets the order, she needs to pass it to the cook. As such both the waitress and the cook may have a method for receiving the order.
  • Receive Pizza: When the cook is done cutting the pizza, he needs to give it to the waitress. The waitress has to respond to the receive pizza method. When the waitress gets the pizza, it must be delivered to the customer, and as such the customer must also respond to the Receive Pizza method.
We come out of this description with a customer object, a cook object, and a waitress object. You can see these in Figure 1. Most likely a full-fledged application would also include a pizza object and an order object, used as data containers as arguments to the methods.

With all these objects, our code changes quite a bit. This line will start the order process:

Customer.OrderPizza();

This assumes that a customer object is already created, of course. The customer OrderPizza function would look like this:

<cffunction name="OrderPizza">
    <cfset Waitress.RecieveOrder(MyOrder)>
</cffunction>

The Waitresses ReceiveOrder method would look like this:

<cffunction name="OrderPizza">
    <cfargument name="Order" type="order">
    <cfset Cook.RecieveOrder(MyOrder)>
</cffunction>

The cook's ReceiveOrder method would look like Listing 2. The cook does a lot of work in this process compared to the customer or waitress. The waitress gets the pizza back from the cook:

<cffunction name=" RecievePizza">
    <cfargument name="pizza" type="Pizza">
    <cfset customer. RecievePizza (arguments.pizza)>
</cffunction>

The customer gets the pizza from the waitress using this method:

<cffunction name=" RecievePizza">
    <cfargument name="pizza" type="Pizza">
    <cfset customer. Devour (arguments.pizza)>
</cffunction>

Instead of one CFC with lots of data and functionality, we have a lot of CFCs with very little data and functionality. The order is passed around, down the chain from the customer to the waitress to the cook. The cook processes it and sends a pizza back to the customer.

Conclusion
A lot of ColdFusion programmers started programming without using any encapsulation at all. Business logic and display code was mixed in a single template. This was partially due to limitations within the language of ColdFusion. When I talk to them they refer to this old school CF method of programming as procedural, even though it had very little to do with procedural programming. I hope this article enlightened you to a few of the differences between procedural programming and object-oriented programming.

About Jeffry Houser
Jeffry Houser has been working with computers for over 20 years and in Web development for over 8 years. He owns a consulting company and has authored three separate books on ColdFusion, most recently ColdFusion MX: The Complete Reference (McGraw-Hill Osborne Media).

CFDJ LATEST STORIES . . .
SQL Injection attacks are one of the easiest ways to hack into a website. One recent hack, using a script from verynx.cn, involves injecting sql into a web form that then appends some JavaScript code into fields in a database that then gets executed on the client side when a user views...
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p...
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T...
CFDynamics, a ColdFusion web host, has renewed an agreement with SmarterTools that will allow them to pass on immediate value to their customers. When a customers signs up for a dedicated hosting account they will now receive $750 worth of features including SmarterMail, SmarterStats a...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE