状态模式---设计任务系统
状态模式定义:
状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
状态模式UML类图:
应用场景:
1、当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
2、一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。这个状态通常用一个或多个枚举常量表示。通常,有多个操作包含这一相同的条件结构。State模式将每一个条件分支放入一个独立的类中。这使得你可以根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其他对象而独立变化。
状态模式分析与实现(as3描述)
package task
{
/**
* 动作 状态
* @author lake
*
*/
public interface IState
{
function receiveTask():void;
function carryOnTask():void;
function finishTask():void;
function waitRecieveTask():void;
}
}
package task
{
/**
* 任务类
* 不同的行为切换不同的状态
* @author lake
*
*/
public class Task
{
/**
*将状态对象组合进来
*/
private var _receiveTaskState :ReceiveTask;
private var _carryOnTaskState :CarryOnTask;
private var _finishTaskState :FinishTask;
private var _waitReceiveTaskState :WaitReceiveTask;
/**
*当前状态
*/
private var _currentState :IState;
public function Task()
{
_receiveTaskState = new ReceiveTask(this);
_carryOnTaskState = new CarryOnTask(this);
_finishTaskState = new FinishTask(this);
_waitReceiveTaskState = new WaitReceiveTask(this);
_currentState = _waitReceiveTaskState;
}
public function set receiveTaskState(value :ReceiveTask):void
{
_receiveTaskState = value;
}
public function get receiveTaskState():ReceiveTask
{
return _receiveTaskState;
}
public function set carryOnTaskState(value :CarryOnTask):void
{
_carryOnTaskState = value;
}
public function get carryOnTaskState():CarryOnTask
{
return _carryOnTaskState;
}
public function set finishTaskState(value :FinishTask):void
{
_finishTaskState = value;
}
public function get finishTaskState():FinishTask
{
return _finishTaskState;
}
public function set waitReceiveTaskState(value :WaitReceiveTask):void
{
_waitReceiveTaskState = value;
}
public function get waitReceiveTaskState():WaitReceiveTask
{
return _waitReceiveTaskState;
}
public function set currentState(value :IState):void
{
_currentState = value;
}
public function get currentState():IState
{
return _currentState;
}
/**
* 接受任务
*
*/
public function receiveTask():void
{
_currentState.receiveTask();
}
/**
* 进行任务
*
*/
public function carryOnTask():void
{
_currentState.carryOnTask();
}
/**
* 完成认为
*
*/
public function finishTask():void
{
_currentState.finishTask();
}
/**
* 等待接受
*
*/
public function waitRecieveTask():void
{
_currentState.waitRecieveTask();
}
}
}
package task
{
/**
* 接任务状态
* @author lake
*
*/
public class ReceiveTask implements IState
{
private var taskObj :Task;
public function ReceiveTask(lTask :Task)
{
taskObj = lTask;
}
public function receiveTask():void
{
}
public function carryOnTask():void
{
trace("正在进行任务");
taskObj.currentState = taskObj.carryOnTaskState;
}
public function finishTask():void
{
}
public function waitRecieveTask():void
{
}
}
}
package task
{
/**
* 进行任务中状态
* @author lake
*
*/
public class CarryOnTask implements IState
{
private var taskObj :Task;
public function CarryOnTask(lTask :Task)
{
taskObj = lTask;
}
public function receiveTask():void
{
}
public function carryOnTask():void
{
}
public function finishTask():void
{
trace("完成任务了");
taskObj.currentState = taskObj.finishTaskState;
}
public function waitRecieveTask():void
{
}
}
}
package task
{
/**
* 完成任务状态
* @author lake
*
*/
public class FinishTask implements IState
{
private var taskObj :Task;
public function FinishTask(lTask :Task)
{
taskObj = lTask;
}
public function receiveTask():void
{
}
public function carryOnTask():void
{
}
public function finishTask():void
{
}
public function waitRecieveTask():void
{
trace("等待接受任务");
taskObj.currentState = taskObj.waitReceiveTaskState;
}
}
}
package task
{
/**
* 等待接受任务
* @author lake
*
*/
public class WaitReceiveTask implements IState
{
private var taskObj :Task;
public function WaitReceiveTask(lTask :Task)
{
taskObj = lTask;
}
public function receiveTask():void
{
trace("接受任务了");
taskObj.currentState = taskObj.receiveTaskState;
}
public function carryOnTask():void
{
}
public function finishTask():void
{
}
public function waitRecieveTask():void
{
}
}
}
package
{
import flash.display.Sprite;
import task.Task;
public class TaskTest extends Sprite
{
public function TaskTest()
{
var taskObj :Task = new Task();
taskObj.receiveTask();
taskObj.carryOnTask();
taskObj.finishTask();
taskObj.waitRecieveTask();
trace("完成一次任务 下一次...");
taskObj.receiveTask();
taskObj.carryOnTask();
taskObj.finishTask();
taskObj.waitRecieveTask();
}
}
}
输出:
接受任务了
正在进行任务
完成任务了
等待接受任务
完成一次任务 下一次...
接受任务了
正在进行任务
完成任务了
等待接受任务