我在寻找的整洁,维护和测试的方式来处理诸如以下,其中不同的参数组合必须以不同的方式由该对象的方法来解释的情况下建议:对象的构造/方法重载
# Every 2 days.
$event = Event::Recurrence->new(recurs => 'daily', interval => 2);
# 1st and 2nd day of every 3rd week.
$event = Event::Recurrence->new(recurs => 'weekly', days => [1, 2], interval => 3);
# 1st and 2nd day of every 4th month.
$event = Event::Recurrence->new(recurs => 'monthly', days => [1, 2], interval => 4);
# 1st and 2nd day of the 2nd and 3rd week of every month.
$event = Event::Recurrence->new(recurs => 'monthly', days => [1, 2], weeks => [2, 3], interval => 1);
# 1st and 2nd day of the 2nd and 3rd week of every year.
$event = Event::Recurrence->new(recurs => 'yearly', days => [1, 2], weeks => [2, 3], interval => 1);
# 1st and 2nd day of the 2nd and 3rd week of the 3rd and 4th months of every 5th year.
$event = Event::Recurrence->new(recurs => 'yearly', days => [1, 2], weeks => [2, 3], months => [3, 4], interval => 5);
# Do something with the event object.
$set = $event->get_set();
get_set()
将根据构造参数的不同而起作用。
我不是在寻找方法来实现日期处理 - 我使用循环事件来说明问题的类型。相反,我正在寻找更好的方法来处理将不同的可能的参数组合分配到合适的方法的更一般的信息。我使用驼鹿,所以驼鹿/ OO模式是受欢迎的。
上述示例可以大致分为不同类型的事件:每日,每周,每月和每年。每个将以不同的方式处理剩余的参数,但最终结果将是相同类型的对象 - 一组可以执行某些操作(获取开始和结束日期,确定交叉点等)的循环事件。
get_set()
因此可以实现一个调度表来处理所有可能的参数组合,为每个参数调用一个单独的方法 - 但是感觉混乱。
我可以用单独的类针对不同复发类型一起创建一个CodeRef属性(Event::Recurrence::Daily
,Event::Recurrence::Weekly
,等等),并指定相应的类,在施工时间属性,类似于接受的答案this question - 虽然我不确定我会如何实现这一点。
为什么要的方法做不同不同组参数的东西?你有没有尝试过一个系统,可以做循环事件,而不必关心他们的时期?在采用这种思维方式太古怪之前,我会寻找一个更简单的解决方案。 –
@brian d foy:我在使用[DateTime :: Event :: Recurrence](http://search.cpan.org/~fglock/DateTime-Event-Recurrence-0.16/lib/DateTime/Event/Recurrence.pm) ,但它不能按照我喜欢的方式处理一些重复。相反,我使用[DateTime :: Set-> from_recurrence](http://search.cpan.org/~fglock/DateTime-Set-0.31/lib/DateTime/Set.pm)来创建循环事件。我想遵循类似于DateTime :: Event :: Recurrence的模式,其中'days','weeks'具有不同的含义,具体取决于是否存在其他参数。例如,'days'可以表示星期几或一个月中的某一天。 – Martin