Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对象';模板方法设计模式中的状态_Php_Design Patterns_Template Method Pattern - Fatal编程技术网

Php 对象';模板方法设计模式中的状态

Php 对象';模板方法设计模式中的状态,php,design-patterns,template-method-pattern,Php,Design Patterns,Template Method Pattern,下面是来自的基础abstract类中算法的实现示例 我不喜欢它,因为我认为“showBookTitleInfo”对它调用的方法知道得太多了 这是另一个例子 抽象类模板法{ var$状态; 公共函数构造(){ $this->state=0; } public function processEvent( $event ) { $this->doFirstStep( $event ); $this->doSecondStep( $event );

下面是来自的基础abstract类中算法的实现示例

我不喜欢它,因为我认为“showBookTitleInfo”对它调用的方法知道得太多了

这是另一个例子 抽象类模板法{ var$状态; 公共函数构造(){ $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;
    }
}
我走的方向对吗? 它是模板方法设计模式应该实现的地方吗? 请让我知道,如果这是错误的代码

p.S.成本计数器不是生产代码。我写它是因为我想给你一个想法


谢谢,模板方法模式提前给了父类很多控制权,父类必须了解很多抽象方法(它们的签名),因为它必须“控制”算法。顺便说一句,父类中的具体方法必须是最终的

你的第一步和第二步方法没有优势,我可以在第一步实现我想要的,而在第二步什么都不做


问题是什么时候要使用模板方法模式,而不是如何重写它以提供更大的灵活性:)

模板方法模式为父类提供了很多控制,父类必须了解很多抽象方法(它们的签名)因为它必须“控制”算法。顺便说一句,父类中的具体方法必须是最终的

你的第一步和第二步方法没有优势,我可以在第一步实现我想要的,而在第二步什么都不做

问题是您希望何时使用模板方法模式,而不是如何重写它以提供更大的灵活性:)

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;
    }
}