When the project requirements are upgraded, you need to add some new code at the end of a member method. How should this be solved? see the example.

for example, the code for the first version is:

//api
public function buyGoods(){
    //
    $validate->validateParam();
    //
    $goods->buy();
}

at this time, the first version is running steadily, and we hope that we will not modify this member method too much, but the requirement of the second version is to add some handling functions after the function has been processed. For example, the code of the second version is:

    //api
    public function buyGoods(){
        //
        $validate->validateParam();
        //
        $goods->buy();
        //
        $user->credit();
        //
        $user->money();
        
        ...n
    } 

this credit () and money () are added later, and if you want to add these functions, you must modify buyGoods (), which destroys the original buyGoods ().
is there a design pattern that allows member methods to be easily extended without changing the original code?


if you don't rely on calls, you can decouple it in mq mode. As a producer in buyGoods, just throw data into mq. Consumers do their own spending.


you can use event ()


to write a new API if you want to keep this API. It doesn't matter whether you change it or not if you don't keep it.


decorator mode?

Menu