这是来自http://sourcemaking.com/design_patterns/template_method/php的基本抽象类中算法的实现示例
public final function showBookTitleInfo($book_in) {
$title = $book_in->getTitle();
$author = $book_in->getAuthor();
$processedTitle = $this->processTitle($title);
$processedAuthor = $this->processAuthor($author);
if (NULL == $processedAuthor) {
$processed_info = $processedTitle;
} else {
$processed_info = $processedTitle.' by '.$processedAuthor;
}
return $processed_info;
}
我不喜欢它,因为我认为“showBookTitleInfo”对它调用的方法了解太多。
这是另一个示例抽象类 template_method { var $state; 公共函数 __construct() { $this->state = 0; }
public function processEvent( $event ) {
$this->doFirstStep( $event );
$this->doSecondStep( $event );
}
abstract public function doFirstStep( &$event );
abstract public function doSecondStep( &$event );
}
class CustomLogic extends template_method {
public function doFirstStep( &$event ) {
echo __METHOD__.": state: ".$this->state." event: $event\n";
$this->state++;
}
public function doSecondStep( &$event ) {
echo __METHOD__.": state: ".$this->state." event: $event\n";
$this->state++;
}
}
如果我们不更改它的值,为什么我们将事件作为引用传递?我应该如何实现“我的步骤”逻辑,如果他们正在使用当前状态,可以修改其值,其他步骤可以读取修改后的值并且也可以修改它?
例如,我想为预定的消息发送实现成本计数机制 - 简单且重复(例如:每个星期一、星期五,直到 2009 年 5 月 23 日)。
所以,我在抽象类中实现算法如下:
abstract class AbstractCostCounter {
public function countNotReccurentSendingCost($messageObj) {
$totalMessages = $messageObj->getTotalMessages(); // multiple recipients are allowed
$message_cost = 1; // just to give you an idea
$this->cost = $totalMessages * $message_cost;
}
abstract public function countOptional();
// I pass $messageObject not as by-reference, because it hasn't to be modified
public function countCost( $messageObject ) {
$this->countNotReccurentSendingCost( $messageObject );
$this->countOptional( $messageObject );
}
}
class TemplateNotReccurentCostCounting {
public function countOptional($messageObj) {
// do nothing
}
}
class TemplateReccurentCostCounting {
public function countOptional($messageObj) {
$notReccurentSendingCost = $this->cost;
$totalMessagesInScheduledPlan = $messageObj->getTotalMessagesInScheduledPlan();
$reccurentSendingPlanCost = $notReccurentSendingCost * $totalMessagesInScheduledPlan;
$this->cost = $reccurentSendingPlanCost;
}
}
我是否朝着正确的方向前进?应该在哪里实现模板方法设计模式?如果这段代码有问题,请告诉我。
PS成本计数器不是生产代码。我写它是因为我想给你一个想法。
提前致谢