python设计模式第二十三天【状态模式】

时间:2022-05-25 01:58:30

1.应用场景

(1)通过改变对象的内部状态从而改变对象的行为,一般表现为状态的顺序执行

2.代码实现

#!/usr/bin/env python
#!_*_ coding:UTF-8 _*_

from abc import ABCMeta, abstractmethod

class Context(object):

    def __init__(self):
        self.state = ConditionOneState(self)

    def request(self):
        '''上下文的请求'''
        self.state.handle(self)

class State(object):

    __metaclass__ = ABCMeta

    def __init__(self, context):
        self.context = context

    @abstractmethod
    def handle(self):
        pass

class ConditionOneState(State):

    def handle(self, context):
        '''状态1的处理'''
        if context.condition == "condition1":
            print "handle condition1"
        else:
            context.state = ConditionTwoState(context)
            context.request()

class ConditionTwoState(State):

    def handle(self, context):
        if context.condition == "condition2":
            print "handle condition2"
        else:
            context.state = ConditionThreeState(context)
            context.request()

class ConditionThreeState(State):

    def handle(self, context):
        '''状态2的处理'''
        if context.condition == "condition3":
            print "handle condition3"
        else:
            context.state = UnkownState(context)
            context.request()

class UnkownState(State):

    def handle(self, context):
        '''未知状态'''
        print "unkown state"

if __name__ == "__main__":
    context = Context()

    context.condition = "condition1"
    context.request()

    context.condition = "condition2"
    context.request()

    context.condition = "condition3"
    context.request()

    context.condition = "condition1"
    context.request()

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23_state/state_test.py
handle condition1
handle condition2
handle condition3
unkown state

Process finished with exit code 0

注意:

(1)处理的过程如下所示:

state1=====condition1========handle1

state1=====condition2========置为下一个状态

state2=====condtion2========handle2

state2=====conditon3========置为下一个状态

state3=====condition3========handle3

state3=====condtion1=========置为下一个状态

unkownstate======condition1=======handle_unkown

3. 实战(使用php的Yii2框架实现库存盘点单据的作废及提示)

业务需求:

(1)若库存盘点已经作废则不能再次作废

(2)若库存盘点单据已经生成报损报溢单据则不能作废提示先删除报损报溢单据

(3)其他情况可以作废

<?php
namespace core\models;

use \core\models\WmsProfitloss;

class WmsCheckDeleterContext{
    /**
     * @var
     * 使用状态模式二实现库存盘点的作废功能; 库存盘点单据的状态有可能为未作废,已生成报损报溢单据且报损报溢单据未作废;库存盘点已经作废
     */
    private $__wmsCheckModel;
    //这里增加状态的引用
    private $__state;

    public function __construct($wmsCheckModel)
    {
        $this->wmsCheckModel = $wmsCheckModel;
        $__isDel = $this->wmsCheckModel->is_del;
        $__wmsProfitlossModelExists = WmsProfitloss::find()->where(['wms_profitloss_union_code'=>$this->wmsCheckModel->wms_check_code, 'is_del'=>0])->exists();

        //这里判断状态类的创建对象
        if ($__isDel){
            $__state = new AlreadyDeletedState($wmsCheckModel);
        }elseif($__wmsProfitlossModelExists){
            if ($__wmsProfitlossModelExists){
                $__wmsProfitlossModels = WmsProfitloss::find()->where(['wms_profitloss_union_code'=>$this->wmsCheckModel->wms_check_code, 'is_del'=>0])->all();
            }
            $__state = new NotDeletedButProfitlossStatus($__wmsProfitlossModels);
        }else{
            $__state = new NotDeletedNotProfitlossStatus($wmsCheckModel);
        }
        $this->__state = $__state;
    }

    /**
     * @param State $state
     * 设置引用的状态对象
     */
    public function setState(State $state){
        $this->__state = $state;
    }

    /**
     * @return AlreadyDeletedState|NotDeletedButProfitlossStatus|NotDeletedNotProfitlossStatus
     * 获取引用的转态对象
     */
    public function getState(){
        return $this->__state;
    }

    /**
     * @return array
     * 作废前准备检测作废状态
     */
    public function requestDeletePrepare(){
        return $this->getState()->prepare();
    }

    /**
     * @return array
     * 作废请求
     */
    public function requestDelete(){
        return $this->getState()->handle();
    }
}

/**
 * Interface State
 * @package core\models
 * 状态类的抽象接口类,定义准备方法和实际处理方法
 */
interface State{
    public function prepare();
    public function handle();
}

class NotDeletedNotProfitlossStatus implements State {
    public function __construct($wmsCheckModel)
    {
        $this->__wmsCheckModel = $wmsCheckModel;
    }

    public function prepare(){
        return ['status'=>true, 'errcode'=>'', 'errmsg'=>'你确定要作废库存盘点单据'.$this->__wmsCheckModel->wms_check_code.'吗?作废后不可恢复'];
    }
    public function handle()
    {
        $deleteResult = $this->__wmsCheckModel->delete();
        if (!$deleteResult){
            return ['status'=>false, 'errcode'=>'', 'errmsg'=>'作废库存盘点单据'.$this->__wmsCheckModel->wms_check_code.'失败'];
        }else{
            return ['status'=>true, 'errcode'=>'', 'errmsg'=>'作废库存盘点单据'.$this->__wmsCheckModel->wms_check_code.'成功'];
        }
    }
}

class NotDeletedButProfitlossStatus implements State{
    public function __construct($wmsProfitlossModels)
    {
        $this->__wmsProfitlossModels = $wmsProfitlossModels;
    }

    public function prepare(){
        $__code_list = \yii\helpers\ArrayHelper::getColumn($this->__wmsProfitlossModels, 'wms_profitloss_code');
        $__code_str = implode(',', $__code_list);
        return ['status'=>false, 'errcode'=>'', 'errmsg'=>'请先作废报损报溢单据'.$__code_str];
    }

    public function handle()
    {
        return $this->prepare();
    }
}

class AlreadyDeletedState implements State{
    public function __construct($wmsCheckModel)
    {
        $this->__wmsCheckModel = $wmsCheckModel;
    }
    public function prepare(){
        return ['status'=>false, 'errcode'=>'', 'errmsg'=>'库存盘点单据'.$this->__wmsCheckModel->wms_check_code.'已经作废,无需再次作废'];
    }
    public function handle()
    {
        return $this->prepare();
    }
}