Page 1 of 1

from inheritance to composition

Posted: Fri Jul 22, 2005 3:57 pm
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?

Posted: Sat Jul 23, 2005 7:28 pm
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

Posted: Wed Jul 27, 2005 8:39 am
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

Posted: Wed Jul 27, 2005 11:32 am
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...