Introduction to Object Oriented Programming in Cold Fusion

Introduction to Object Oriented Programming in Cold. Fusion Nicholas Tunney Senior Software Architect

Why Am I Listening to This Guy? • Experience • Credentials – – – Articles and Whitepapers Presentations Adobe Community Expert Adobe Certified Advanced Cold. Fusion Developer Adobe Certified Instructor • Current Projects

What will we cover? • • • Why use Object Oriented concepts? Introduction to Object Oriented Programming How to apply Object Oriented principles to Cold. Fusion Samples Q and A

Why use Object Oriented Concepts • • Scalability Uniformity Reusability Maintainability

Introduction to Object Oriented Programming • History (Bar trivia) – Simula I and Simula 67 (early 60 s) – Small. Talk used Simula concepts (early 70 s) • Abstract datatypes • Concurrent program execution • Integrated GUIs – C++ and Java

Introduction to Object Oriented Programming • Cold. Fusion is not pure OO! – Pure OO languages treat everything in the language as an object – Cold. Fusion allows us to use tools within the language that apply OO concepts, but does not require the use of objects – Constructors, Overloading – “Java Lite", Rails.

Introduction to Object Oriented Programming • OO is not a language or a framework – Principles – Best Practices – Patterns

Introduction to Object Oriented Programming • Class – Properties – Constructor – Accessors/Mutators – Object Methods – Sometimes Data Access (CRUD)

Introduction to Object Oriented Programming • Object – Instantiated class – Real world entity

Introduction to Object Oriented Programming • Defining an object - “Be” the object – Who can I talk to? – Who do I depend on? – What can I do?

Introduction to Object Oriented Programming • Encapsulation – Private properties – Accessors/Mutators – Good API defines the object. – Over Engineering

Introduction to Object Oriented Programming • Polymorphism – One class, many instantiations – Employee class • Manager • Accountant • Programmer

Introduction to Object Oriented Programming • Inheritance and Composition – The “is a” “has a” paradigm • Employee – Employee has a(n) email address – Manager is a(n) employee – “Favor Composition”

Introduction to Object Oriented Programming Q&A

Applying Object Oriented Principles to Cold. Fusion • Creating a class – <cfcomponent> <cfcomponent name=“Employee”> … properties … … constructor … … accessors/mutators … … public methods … … private methods … </cfcomponent>

Applying Object Oriented Principles to Cold. Fusion • Creating a class – Properties <!--- properties ---> <cfscript> variables. id = 0; variables. first. Name = “”; … </cfscript> -- OR – <cfset variables. id = 0 /> <cfset variables. first. Name = “” /> …

Applying Object Oriented Principles to Cold. Fusion • Creating a class – Constructor <cffunction name=“init” access=“public” returntype=“Employee” output=“false”> … <cfreturn this /> </cffunction>

Applying Object Oriented Principles to Cold. Fusion • Creating a class – Accessor (“Getter”) <cffunction name=“get. ID” access=“public” returntype=“numeric” output=“false”> … calculations … <cfreturn variables. id /> </cffunction>

Applying Object Oriented Principles to Cold. Fusion • Creating a class – Mutator (“Setter”) <cffunction name=“set. ID” access=“public” returntype=“void” output=“false”> <cfargument name=“id” required=“true” type=“numeric” /> … calculations … <cfset variables. id = arguments. id /> </cffunction>

Applying Object Oriented Principles to Cold. Fusion • Creating a class – public and private methods – Data Access – CRUD – The “What can I do” of the object

Applying Object Oriented Principles to Cold. Fusion • Instantiating an object – best practice <cfset my. Object = create. Object(“component”, path_to_cfc). init() /> Example: <cfset Carl = create. Object(“component”, “cfc. Employee”). init(1, “Carl”) />

Applying Object Oriented Principles to Cold. Fusion • Interacting with properties – Properties are in private scope (encapsulation) – Use accessor/mutator methods <cfset Carl. set. First. Name(“Carl”) /> <cfset first. Name = Carl. get. First. Name() />

Applying Object Oriented Principles to Cold. Fusion • Persistence Methods – CRUD (Create(), Read(), Update(), Delete()) – Can also be separated out into separate DAO – CRUD can be further encapsulated with commit()

Applying Object Oriented Principles to Cold. Fusion • Gateway Objects – Used for accessing multiple objects or records – For small applications, methods can be put into one main data access object – For large applications, best practice is to move methods into object specific gateways

Conclusion • OO is not new, just new to CF • CF is not pure OO • Classes contain encapsulated properties, accessor and mutator methods, private and public methods • Objects are instantiated classes • Objects are programmatic representations of real world entities

Conclusion - cont’d • • Think Objects instead of Relational DB. “Be” the object when defining the class Composition is preferred to inheritance DAO methods may be in the class, or in its own class (CRUD) • Gateways access recordsets or multiple objects • Code generators can be your best friend!

Q&A

Thanks! http: //www. aboutweb. com http: //www. nictunney. com http: //www. cfoop. org
- Slides: 28