Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Discussion: improve a simple OOD (PHP/MySQL)

Status
Not open for further replies.

LocoCoco

Programmer
Sep 20, 2008
2
I'm on my way familiarizing myself with object oriented design in PHP (MySQL). I wanted to realize a very simple and stupid example to see how it's supposed to work, but it turned out being more complex than I thought.

This is what I want to realize:
Basically I have Students that can do Tasks from various categories.
When a Student calls the page (index.php) he should be asked to enter his/her name.
PHP opens a MySQL connection and gets the Students ID from the Database.
Then all the available Categories are retrieved from the Database.
Retrieve all Tasks to the Categories.
Check if this Student with this particular ID has done this task, output yes if so, otherwise no.

I came up with the following UML class diagram (I hope I didn't misinterpreted the conventions)

umlxk9.jpg


The class Workshop is the central class that holds all the other classes (aggregation). The constructor from Workshop is called when the site gets loaded. Workshop instantiates and initializes an object from the class Student. An object of CategoryArray is initiated and initialized with all the categories in the database, every Category Object in the Array gets its tasks from the database (this works by passing the Category string to each category object on creation, so that it knows what category it has to take care of).
The Workshop iterates through the CategoryArray object as well as the CategoryArray iterates through the Array of Tasks (would it make sense to add a TaskArray class?). Every task gets checked whether it has been solved by this student or not.

Is this how an object oriented design would look like or is it totally useless? I'd like to discuss with the users of this forum to get valuable input. I appreciate suggestions on how to improve my solution, I'm not asking anyone to do it for me since I'd like to learn how to really think object oriented - but I need to know if I'm on track!

I hope someone has time to think about it for a moment and shares his/her opinion.

Thank you in advance
coco

I've read the Posting Policy, and my contribution/question to this forum has absolutely nothing to do with homework!
 
Hi, LocoCoco

I think you got the idea. The few remarks I have are related to advanced oo design techniques, and design patterns.

Do you really need the CategoryArray class? Is not possible to use the same mechanism as for the task? you don't have a task list object and I don't think you need one.

Obviously you need a structure to keep some elements, but why to be a TaskArray or a CategoryArray. After all an array is an array an a list is a list no mater what elements contains. You can take a look on iterator pattern for that, of just use a simple array.

I think it's a good idea to take a look on the builder design pattern in order to find out how you can generate the structure.

You might also want to use the concept of lazy loading to instantiate the objects only when you need them.

 
Thank you jjbo
I really appreciate your posting, the kind of information that is valuable to me.

Well you're right I should substitute the CategoryArray class. My first problem there is I can't get my head around this, I mean when I want to use an Iterator I wouldn't create an Array would I? I've read about the Iterator pattern, know what it does but how would I apply it to this particular problem?

The easiest way would be to have an array in Workshops that holds all categories, and every category object has an array of Tasks. But I'm quite sure this is not a clean solution as you mentioned. Where would the Iterators come in place?

I just see a TaskIterator, where would the CategoryIterator be? How would the UML look if I'd like to realize it with Iterators?

I used an Iterator to go through my MySQL result, but there I don't have an Array I've just got a ressource I have to go through but using Iterators on Arrays doesn't make sense.

Considering the following code: (that's one of my problems I have, I can't design I always tend to think HOW it is being done in the code)

Code:
$categoryObjects = array(new Category("Analysis"),new Category("Algebra"));

foreach($categoryObjects as $catobj) {
   $taskIterator =  new TaskIterator($catobj->getTasks());
   foreach($taskIterator->fetch() as $task)
   {
       echo $task->getTaskTitle();
   }
}

Does this make sense? Where in the UML diagram would the Iterator "TaskIterator" be?

I've also read about the builder design pattern and the lazy loading but I'm going to think about that once I'm used to the basics.

Thanks in advance
coco
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top