from inheritance to composition

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

from inheritance to composition

Post by andre_c »

i have a Schedule object that is extended by a WeeklySchedule and a MonthlySchedule...

Code: Select all

<?
$schedule = new WeeklySchedule;
$schedule->addEvent( $event );

$html = $schedule->getHTML();
?>
Inheritance was a bad choice.

I want to change the design to something like this:

Code: Select all

<?
$schedule = new Schedule;
$schedule->addEvent( $event );

$html = $schedule->getHTML('WEEK');
$html2 = $schedule->getHTML('MONTH');
?>
but i want to use a different classes for WEEK and MONTH...

any thoughts?


i'm trying to decide between

Code: Select all

<?
WeeklySchedule::getHTML( $schedule );
?>

or something like:

Code: Select all

<?
class Schedule {
...
  public function getHTML( $type ) {
    $printer = new "$type_Schedule";
    return $printer->getHTML($this);
  }
...

}
?>

can you think of a better way?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Why was the inheritancce a bad choice?

Is it because you need to have a notion of "Scheduled Events" and then a choice of "Display Formants"

Perhaps you should look at splitting the responsibility to a "Schedule" class that handles add events, removing events, and what ever other business logic you need. And then a "CalendarFormat" Interface which both your Monthly/Weekly ones can implemenent.

Then depending on usage you can either pass a Schedule to a WeeklyFormatter, or pass a WeeklyFormatrer to Schedule->Display($formater), etc
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Strange for me.

If you check his CV posted online you should see that he knows C++ so this kind
of analisys should be a joke for him :P
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

nielsene wrote: ...Then depending on usage you can either pass a Schedule to a WeeklyFormatter...
... that's exactly what i ended up doing, thanks
fastfingertips wrote: Strange for me.

If you check his CV posted online you should see that he knows C++ so this kind
of analisys should be a joke for him
eh...
Post Reply