Posted At : August 28, 2006 6:22 AM
Why is Object Oriented code worth learning:
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:
For example, a developing a typical e-commerce website you might have a product object.
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:
What does it not know:
What can it do:
What can it not do:
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:
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
Numerous functions that generate query results of different aspects of the products. For example:
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.
0 Comments