Burada http://sourcemaking.com/design_patterns/template_method/php arasındaki temel absctract sınıfta algorigthm bir uygulama örneği
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;
}
Sanırım becase ben "showBookTitleInfo" çağıran yöntemleri hakkında çok fazla biliyor, bunu sevmiyorum.
Here is another example abstract class template_method { var $state; public function __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++;
}
}
why we pass event as by-reference, if we don't change its value? How should I implement "my steps" logic, if they are using current state, can modify its value, and other steps can read modified value and can modify it too?
Basit ve tekrarlayan (: 23.05.2009 yılına kadar her Mon, Fri eski) - Örneğin, ben göndermek planlanan mesajı için maliyet sayma mekanizması uygulamak istiyor.
Yani, aşağıdaki gibi soyut sınıfta algoritma:
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;
}
}
Am I moving in the right direction? Is it where Template method design pattern should be implemented? Please let me know, if it is something wrong with this code.
P.S. maliyet sayacı üretim kodu değil. Ben size bir fikir vermek istedim, çünkü ben yazdım.
Teşekkürler, peşin