views:

297

answers:

2

Please help me to implement Factory design pattern for the task.

I working on scheduled message sending web application. User can specify his own scheduled sending plan, and messages will be delivered when it have to. User can choose only one of 3 reccuring types: monthly, weekly, daily. For this 3 reccuring types algorithm is similar: 1) choose send date and time(for first delivery) 2) choose option in "repeat every X months/weeks/days" depending on reccuring type 3) specify end by type(date or by occurences limit)

Weekly scheduler is a little bit different. It has one more feature - week days for repeating(ex: repeat every Mon, Fri)

class SchedulerFactory {
    public function create($type) {
        switch ($type) {
            case "monthly" :
                $Scheduler = new MonthlyScheduler();
     break;
            case "weekly" :
                $Scheduler = new WeeklyScheduler();
     break;
            case "daily":
                $Scheduler = new DailyScheduler();
     break;
            default:
                return null;
        }
    }
}

Setter methods:

$SendScheduler->setSendDateAndTime("{$day}.{$month}.{$year}", time());

switch($end_by_type) {
 case 'by_date':
     $SendScheduler-     >setEndByDate("{$end_by_date_day}.{$end_by_date_month}.{$end_by_date_year}");
    break;
case 'by_occurences_limit':
    $SendScheduler->setOccurencesLimit($occurences_quantity);
    break;      
}

$SendScheduler->addTaskOccuringEvery($RepeatEveryMonth);// repeat every X months/weeks/days depending on type

// only for weekly type
$SendScheduler->setWeekDayForRepeating($week_day); // repeat every Mon, Fri

I want to use Factory pattern for object construction(Is it the best match?), and don't know how can I pass all this parameters(not just $type, but all other) for object construction.

Should I create wrap them in single data object, and create method "setOptions" in the SchedulerFactory class and pass this data object to "create" construction method through getter?

Maybe creating of such setter method in Factory class is violation of Factory design pattern principles? I didn't see an examples like this before, so I'm a bit confused now.

If I'm going in absolutely wrong direction, please let me know. What solution would you recommend for this task?

Thanks, in advance

+1  A: 

You could create a wrapper object for the scheduling options and pass that along with the type to the SchedulerFactory.create() method. In your factory method you would then create scheduler objects and set properties based on the types so everything is encapsulated in the Scheduler Factory. I suppose you could also do something messy and use PHP's variable method arguments instead of an object but that would get ugly.

I would avoid creating a setOptions method as this would require an instance of SchedulerFactory where passing the options as an object allows the factory (and create method) to remain static.

To add to vartec's post, since it appears from your design and use of the Factory pattern the Scheduler subclass properties are only read after the objects are created, you may want to make each Scheduler immutable by allowing the properties to only be set at the time the object is created (in the constructor).

aurealus
+1  A: 

I think you misunderstood the factory pattern. Factory should only create object instances. As for passing more parameters you can do:

class Scheduler {
    public function factory($type, $paramsArr = array()) {
        switch ($type) {
            case 'monthly' : $className = 'MonthlyScheduler'; break;
            case 'weekly' : $className = 'WeeklyScheduler'; break;
            case 'daily' : $className = 'DailyScheduler'; break;
            default: return null;
        }
        $sched = new ReflectionClass($className);  
        return $sched->newInstanceArgs($paramsArr);  ;
    }
}

where $paramsArr is array of constructors parameters

thus:

$yourSched = Scheduler::factory('weekly',array('Monday'));

is equivalent to:

$yourSched = new WeeklyScheduler('Monday');
vartec