我不知道这样的框架(这并不意味着它不存在)。但是,虽然不像链接框架那样具有丰富的功能,但状态模式的实现相当简单。考虑下面这个幼稚的实现:
interface EngineState
{
public function startEngine();
public function moveForward();
}
class EngineTurnedOffState implements EngineState
{
public function startEngine()
{
echo "Started Engine\n";
return new EngineTurnedOnState;
}
public function moveForward()
{
throw new LogicException('Have to start engine first');
}
}
class EngineTurnedOnState implements EngineState
{
public function startEngine()
{
throw new LogicException('Engine already started');
}
public function moveForward()
{
echo "Moved Car forward";
return $this;
}
}
定义状态后,只需将它们应用于主对象:
class Car implements EngineState
{
protected $state;
public function __construct()
{
$this->state = new EngineTurnedOffState;
}
public function startEngine()
{
$this->state = $this->state->startEngine();
}
public function moveForward()
{
$this->state = $this->state->moveForward();
}
}
然后你可以做
$car = new Car;
try {
$car->moveForward(); // throws Exception
} catch(LogicException $e) {
echo $e->getMessage();
}
$car = new Car;
$car->startEngine();
$car->moveForward();
对于减少过大的 if/else 语句,这应该足够了。请注意,在每次转换时返回新的状态实例有些低效。就像我说的,这是一个幼稚的实现来说明这一点。