×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Discussion: improve a simple OOD (PHP/MySQL)

Discussion: improve a simple OOD (PHP/MySQL)

Discussion: improve a simple OOD (PHP/MySQL)

(OP)
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)



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!

RE: Discussion: improve a simple OOD (PHP/MySQL)

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.

www.oodesign.com

RE: Discussion: improve a simple OOD (PHP/MySQL)

(OP)
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
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close