OO 101 : part 1

Comments

gjuutydhg lkgkj kiuf hgku

Posted By: mmmm On: 03/08/09 8:45 PM

zsgsrgh

Posted By: Louis Vuitton Handbags On: 03/11/11 1:49 AM

Add Comment

Comments have been closed.

Transcript

no image

Slide Text

Slide Notes


OOP 101


OOP 101
TACFUG / John Whish
25.02.2009

no notes exist for this slide

The History of CFCs


The History of CFCs
7 Years old next month!
CFCs where introduced in ColdFusion MX6, but weren't very well implimented
In ColdFusion MX6.1 (a free update) they introduced the power of OOP to ColdFusion developers
ColdFusion MX 7 introduced Application.cfc
ColdFusion 8 (yes the MX has been dropped) introduced cfinterface.

no notes exist for this slide

Why Use CFCs


Why Use CFCs?
Code re-use
CFCs are similar to custom tags but with multiple entry points and ability to hold their own data
Black boxes
Developers shouldn’t care about implementation details “under the hood”.
A developer only needs to know the interface.

no notes exist for this slide

Slide 4


Why Use CFCs?
Organisation
CFCs can be organized into a hierarchy of packages.
Remote Calls
Expose your business logic as a webservice for AJAX, Flex, SaaS
Object Orientated
CFCs bring the power of Object Oriented Programing to ColdFusion

no notes exist for this slide

Who Uses CFCs


Who Uses CFCs?
CFCs are the foundation of most modern frameworks.
Coldbox
Fusebox
Model-Glue
Mach II
Transfer
Reactor
ColdSpring
Etc

no notes exist for this slide

What Can a CFC do


What Can a CFC do?
Data
A CFC holds it's own data (like a structure) for it's life-time
Behaviour
Provides mechanisms for acting on it’s data (functions)
Self Documenting
ColdFusion can generate documentation for you!
(Using CFC Explorer, cfdump and GetMetaData)

no notes exist for this slide

Basic Static Class Example


Basic Static Class Example!
Person.cfc
<cfcomponent hint="I represent Joe Bloggs" output="false">
    <cffunction name="SayMyName" access="public" output="false">
        <cfreturn "My name is Joe Bloggs" />
    </cffunction>
</cfcomponent>
Using the Person component
<cfoutput>
#CreateObject("component", "Person").SayMyName()#
</cfoutput>
Not very useful! This type of class is called a static class as he doesn't hold any data.

no notes exist for this slide

Better Class Example


Better Class Example
Person.cfc
<cfcomponent hint="I represent a person" output="false">
    <cffunction name="sayMyName" hint="I return name" output="false">
        <cfreturn "My name is " & variables.name />
    </cffunction>
    <cffunction name="setName" hint="I set the name" output="false">
        <cfargument name="name" required="true" />
        <cfset variables.name = arguments.name />
    </cffunction>
</cfcomponent>
Using the Person component
<cfset Fred = CreateObject("component", "Person") />
<cfset Fred.setName( "Fred Bloggs" ) />
<cfoutput>
#Fred.sayMyName()#
</cfoutput>

no notes exist for this slide

The Constructor


The Constructor
Most OO languages have constructors. ColdFusion doesn't so we use a pseudo constructor, typically called init.
Example 1:
<cfcomponent output="false">
    <cffunction name="init" hint="I act as the constructor" output="false">
        <cfset variables.created = Now() />
        <cfreturn this />
    </cffunction>
</cfcomponent>
Example 2:
<cfcomponent output="false">
  <cfset variables.created = Now() />
</cfcomponent>
Example 1 is preferred, as we can pass parameters (and validate if required)

no notes exist for this slide

Slide 10


The Constructor
Why return "this"?
<cfcomponent output="false" hint="I represent a Person">
    <cffunction name="init" 
    hint="I act as the constructor" 
    output="false"
    returntype="Person">
        <cfreturn this />
    </cffunction>
</cfcomponent>
We can create an initialise all in one line
- Thus, avoiding race conditions
Why can enforce the datatype
- Not so useful in a language that compiles at runtime

no notes exist for this slide

Slide 11


The Constructor
Person.cfc
<cfcomponent hint="I represent a person" output="false">
    <cffunction name="init" hint="I act as the constructor" output="false">
        <cfargument name="name" required="true" />
        <cfset setName( arguments.name ) />
        <cfreturn this />
    </cffunction>
     <cffunction name="sayMyName" hint="I return name" output="false">
        <cfreturn "My name is " & variables.name />
    </cffunction>
    <cffunction name="setName" hint="I set the name" output="false">
        <cfargument name="name" required="true" />
        <cfset variables.name = arguments.name />
    </cffunction>
</cfcomponent>
Using the Person component constructor
<cfset Jane = CreateObject("component", "Person").init( "Jane Doe" ) />
<cfoutput>#Jane.sayMyName()#</cfoutput>

no notes exist for this slide

Terminology


Terminology
Class
In ColdFusion, this is a .cfc file, with cfcomponent tags.
<cfcomponent>
    ...my code here...
</cfcomponent>
A class...
is a blueprint / template for an object.
defines the data the object has.
defines what the object can do.

no notes exist for this slide

Slide 13


Terminology
Object
An object is an instance of a class.
<cfset Jane = CreateObject("component", "Person").init() />
<cfset Jane.setFullname( "Jane Doe" ) />
<cfset Fred = CreateObject("component", "Person").init() />
<cfset Fred.setFullname( "Fred Bloggs" ) />
Both Jane and Fred are instances, created from the same class, Person.cfc. 
Although they have the same behaviour, they have different data (name).

no notes exist for this slide

Slide 14


Terminology
Method (aka: functions)
In ColdFusion, methods are created using cffunction tags. Some other names you'll hear for methods are:
accessors / mutators
getters / setters
Methods allow you to get the object's data and set the object's data. They define the object's behaviour.
Attributes (aka: fields, instance data, properties)
An object has attributes. This is the data held in the object. For example: first name, DOB etc.

no notes exist for this slide

Anatomy of a CFC


Anatomy of a CFC
<cfcomponent>
Defines a component (a Class)

<cffunction>
Defines a function (a Method)
<cfargument>
Defines a parameter of a function

<cfreturn>
Returns results from a function

no notes exist for this slide

Slide 16


Anatomy of a CFC
CFCs are wrapped in a <cfcomponent> tag.

All CFC code is placed inside the <cfcomponent> tag.
CFCs must have a â€œ.cfc” extension.
Only one component per â€œ.cfc” file.

no notes exist for this slide

ltcffunctiongt attributes


<cffunction> attributes
Name
Required, specifies the function’s name
Access
Options are: public, private, package, remote
Hint
Developer provided documentation about the function
Output
When true, the function behaves like a <cfoutput> tag
ReturnType
Indicates the type of data which will be returned by the function

no notes exist for this slide

ltcfargumentgt attributes


<cfargument> attributes
Name
Required, specifies the parameter’s name
Required
Indicates it the argument must be passed
Hint
Developer provided documentation about the argument
Default
Value to use if the argument is not passed (not required)
Type
Indicates the type of data which can be passed.

no notes exist for this slide

Scoping


Scoping
This Scope
<cfset this.id =  "" />
Available to all methods of the class and can be got or set from outside the object.
Variables Scope
<cfset variables.id = "" />
Available to all methods of the object.
Local Variables
<cfset var id = 0 />
Only available inside the function.

no notes exist for this slide

Lets run some code


Let's run some code!
Modelling a Person as a class

no notes exist for this slide

Credits


Credits
This presentation has been largely based on the OO Code Camp materials created for the TACFUG.
OO Code Camp was originally started by the Triangle Area ColdFusion User Group (tacfug.org) in 2009. Dan Wilson (nodans.com) and Jim Priest (thecrumb.com) led the initial course.
The presentation materials were graciously donated by Doug Hughes (alagad.com) and modified slightly to fit the format of the course.
A great thanks to all who taught us what they knew, so we might go forth and teach others.
I am solely responsible for any bugs/errors in the accompanying code!

no notes exist for this slide

Licence


Licence
This presentation and accompanying materials are licensed under the GPL version 2.
You may use this presentation and accompanying materials in whole or in part, as long as you leave in the Credits Slide and the License Slide and you do not charge for whatever course, event, conference you use aforementioned materials in.

no notes exist for this slide