| 
	
 | 
 Posted by David Haynes on 11/02/05 05:28 
Mark ??;-) wrote: 
> I may be one of those people that have problems converting to the OOP way of  
> thinking.  I understand the car example and understand the idea of objects  
> doing things.  Where I seem to be having the problem is in trying to convert  
> an exsisting Structured Programming style program into OOP.  My program  
> basically allows users to upload files and manage them.  I have created  
> files as an object and this seems to be OK, my problem is dealing with the  
> display of the files.  This requires SQL queries and HTML tables.  How  
> should I be thinking of these items in terms of OOP? 
>  
> Am I causing myself more problems by trying to convert from an existing  
> program?  Is there a good way to get started on determining which objects to  
> create? 
>  
> Thanks, 
>  
> -Mark 
 
One quick way to think about conversion is that objects are all the  
nouns and methods are all the verbs. This is somewhat simplistic but  
often is a good way to start. 
 
When you say you have encapsulated the files as an object, does this  
mean that you have a file object or that you have objects that access  
files? In the first example, you would have generic methods (read,  
write, delete, create) that concerned all files as a generic type. In  
the second example, file access would simply be a product of satisfying  
a method on an object. 
 
Put another way - are you sure that 'file' is an appropriate object when  
talking about 'display' methods? I think it is more likely that you want  
to display a specific instance of a file or some object that simply uses  
a 'file' to satisfy some of its methods. 
 
For example: The 'car' object may use a file object, a database object  
and an HTML object to assist in the implementation of certain methods,  
but the real object (noun) used by the system is 'car'. We would want to  
have methods like 'save', 'load' and 'toHTML' as file, database and  
display methods of the object 'car'. 
 
e.g. 
// instantiate a new car object 
$car = new Car(); 
 
// read in the data for an Acura 3.2 TL 
$car->load('Acura 3.2 TL'); 
 
// make the car red 
$car->setColor('red'); 
 
// display all the data including the color as HTML 
$car->printHTML(); 
 
Now the 'load' method may actually use the File object to read in the data. 
 
e.g. 
 
function load($model) { 
    $file = new File(); 
    try { 
        $this->carData = $file->read($model); 
    } catch( Exception $e ) { 
        ... 
    } 
} 
 
As for conversion of structured code, it really comes down to how well  
you can divorce the intent of the code from the implementation of the  
code. What is the code really trying to achieve? What objects is it  
really dealing with? How may these be implemented in an object model and  
what methods does each object then need to support? 
 
Try taking one path through the existing code and seeing which objects  
and methods would make sense. Now add another path. Does your model  
change and how? 
 
-david-
 
  
Navigation:
[Reply to this message] 
 |