私が(具体的なことを知らないうちに)できることは、各状態の一連のクラスを構築することです。そして、そのクラスにdoStuffを押す:
class DoStuff { //The Client
protected $strategies = array();
public function addStrategy(iDoStuffStrategy $strategy) {
$this->strategies[] = $strategy;
}
public function doStuff ($a, $b, $c, $d) {
foreach ($this->strategies as $strategy) {
if ($strategy->test($a, $b, $c, $d)) {
return $strategy->doStuff();
}
}
throw new RuntimeException('Unhandleable Situation!');
}
}
interface iDoStuffStrategy {
// Return a bool if you can handle this situation
public function test($a, $b, $c, $d);
// Execute the implementation
public function doStuff();
}
その後、各クラスには、次のようになります。
public function StrategyFoo implements iDoStuffStrategy {
public function test($a, $b, $c, $d) {
return $a && $b && $c && $d;
}
public function doStuff() {
//DoStuff!
}
}
public function StrategyBar implements iDoStuffStrategy {
public function test($a, $b, $c, $d) {
return !$a && $b && $c && $d;
}
public function doStuff() {
//DoStuff!
}
}
それは基本的にStrategy Patternの実装です。そのようにすれば、意思決定ツリーを分けることができます。
if文が少なくて済みます。真剣に、この質問はあまりにも曖昧です –
よくスイッチを使用しないでください。 –
ここでは 'と'は(スイッチの内部で)動作するとは思わない。 '.'を使う? http://コードパッド。org/X4fIQsSz – Dogbert