State Machine Pattern of PHP Design Patterns - Realizing Business Flow Control

Application scenario : In our daily development, we often encounter various status switches, such as the order status of the e-commerce system. If we disperse various states in various places, it is not conducive to management, and secondly, once a problem occurs, it is difficult to find the cause and repair, and it is often prone to various bugs, and the control is not rigorous. At this time, we need to use the state machine mode to reasonably control the verification, processing and change of the order status. 
Example environment : PHP framework YAF, which can actually be used in various other environments 

Without further ado, let's go directly to the code to implement the simplest state machine pattern:

 

class Order_OrderFlowService{
    //Define constants, five states of the order
    const ORDER_STATUS_NEW = 'new';
    const ORDER_STATUS_PAYED = 'payed';
    const ORDER_STATUS_DELIVERY = 'delivery';
    const ORDER_STATUS_RECEIVED = 'received';
    const ORDER_STATUS_COMPLETE = 'complete';

    //Single entry, unified management of state changes
    public static function changeStatus($status, $data){
        //do some validation before return
        return self::dispatch($status, $data);
    }

    //State machine distribution processing
    private static function dispatch($status, $data){
        switch($status) {
            case self::ORDER_STATUS_NEW: $result = self::createNewOrder($data); break;
            case self::ORDER_STATUS_PAYED: $result = self::orderPayed($data); break;
            case self::ORDER_STATUS_DELIVERY: $result = self::orderDelivery($data); break;
            case self::ORDER_STATUS_RECEIVED: $result = self::orderReceived($data); break;
            case self::ORDER_STATUS_COMPLETE: $result = self::orderComplete($data); break;
            default: $result = false;
        }
        return $$result;
    }

    // respective function implementation
    private static function createNewOrder($data){
        //create new order
    }

    private static function orderPayed($data){
        //make the order payed
    }

    private static function orderDelivery($data){
        //make the order delivery
    }

    private static function orderReceived($data){
        //make the order reveived
    }

    private static function orderComplete($data){
        //make the order complete
    }

}

 First, we define five preset fields of order status for convenience. Then write the unified entry method changeStatus. In this method, we can do unified processing and permission verification, which is omitted in the above example. The unified entry calls the distribution mechanism to match the corresponding method to process the corresponding work. In this place, we can expand the function of the state machine method according to the business needs, and can check the state or process the data in the logic of distribution matching, or put the check into the specific business method.

 

 

related resources:

http://lampblog.org/1817.html

http://yohan.giarel.li/Finite/

https://github.com/phwoolcon/fsm

https://github.com/winzou/state-machine

http://www.php.cn/linux-370234.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326335404&siteId=291194637