Object Orientation for CF Developers
Why?
Why is Object Oriented code worth learning:
- reliability - possible to test each component
- maintenance - it is easy to know where things should be
- encapsulation - knowing where the hard bits are
- reuse - all code is in libraries not pages of your site
How?
For me the basic rule of object orientation is to be able to visualise what it is I'm dealing with so that I can ask the questions:
- What does it know?
- What can it do?
For example, a developing a typical e-commerce website you might have a product object.
A Single Product
Now the product knows certain things, and can do certain things, and many other things it doesn't know and can't do. The essence of good object oriented design is getting this right.
A product object is responsible for a single product - not more than 1 product. A product object will always be instantiated - i.e create a specific instance of an object. In CF instantiation is typically done by calling an init() method to initialise/instantiate the product.
In this case lets talk about specific instance of a product object, a book called "The DaVinci Code"
What does the Product Object Instance - "The DaVinci Code" know:
- It's price
- Name
- Author
- Publisher
- Date of publication
- ISBN number etc
- Product type - eg book
What does it not know:
- The amount of sales tax that will be applied to it - which may vary by location sold etc
- The cost of postage for this item to different locations
What can it do:
- Edit it's details, name, ISBN etc
- Delete itself
- Get it's publisher object
- Get it's author object
- Check stock available
What can it not do:
- Create new product
Dealing with multiple products
For ease of logic I like to seperate the functionality that is not specific to a single object into an objectManager class. In this case productManager and it has the responsibility for managing all the products.
Unlike a product object - the productManager object will not be instantiated - i.e. it will not have an init method
It will have methods that allow you to:
- Search the products with different critera
- Report on products - sales, stock etc
- Create new products
Searching Products
This is a flexible interface to search products based on different critera - eg price, name, location. It is designed in such a way that adding new critera to it does not affect previously written code in our application. The detail of the searchProducts functionality is beyond the scope of this article
Reports
Numerous functions that generate query results of different aspects of the products. For example:
- Stock report
- Sales report by product
- Products by location
- Products by type etc
Creating new products
In order to create a new Product object we place the createProduct code in a productManager.cfc which has responsibility for dealing with multiple products, or in this case products that don't exist yet.
So when we call createProduct on productManager.cfc we specify some basic settings and it will return a product instance with these settings.
In summary:
Get a visual picture of each object you are dealing with - make if concrete (i.e. specific detail) in order see what makes sense for this product to know and not know.
- Product - single item
- Product Manager - multiple items & creation




There are no comments for this entry.
[Add Comment]