PHP design patterns state mode code examples (29)

Time:2020-11-21

objective

State patterns can encapsulate different behaviors based on the same transaction of an object. It provides a simple way to change the behavior of an object at runtime without the aid of a single huge conditional judgment statement.

UML diagram

The official PHP advanced learning exchange community “click” manages and sorts out some materials. Bat and other first-line factories have advanced knowledge systems (related learning materials and written test questions) and are not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, swote, soft, Kafka, MySQL optimization, shell script, dock Er, micro service, nginx and other knowledge points

code

  • ContextOrder.php
done();
    }

    public function getStatus(): string
    {
        return static::$state->getStatus();
    }
}
  • StateOrder.php
details['status'] = $status;
        $this->details['updatedTime'] = time();
    }

    protected function getStatus(): string
    {
        return $this->details['status'];
    }
}
  • ShippingOrder.php
setStatus('shipping');
    }

    protected function done()
    {
        $this->setStatus('completed');
    }
}
  • CreateOrder.php
setStatus('created');
    }

    protected function done()
    {
        static::$state = new ShippingOrder();
    }
}

test

  • Tests/StateTest.php
setState($order);
        $contextOrder->done();

        $this->assertEquals('shipping', $contextOrder->getStatus());
    }

    public function testCanCompleteShippedOrder()
    {
        $order = new ShippingOrder();
        $contextOrder = new ContextOrder();
        $contextOrder->setState($order);
        $contextOrder->done();

        $this->assertEquals('completed', $contextOrder->getStatus());
    }
}

PHP Internet architect’s way to grow * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuously updated)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to communicate and learn with more senior developers, you can get more technical advice and guidance related to interview with large factories. Welcome to join our group, code: phpzh (junyang number: 856460874).

The latest PHP advanced tutorial 2020, full series!

If the content is good, I hope you can support and encourage you to click like / like, and you are welcome to exchange views. In addition, if you have any questions, you can suggest the content you want to see in the comments

Recommended Today

Explain module, import and export in JavaScript

Author: Tania rascia Crazy technology house Original text:https://www.taniarascia.com/j… In the era of the Internet, websites are mainly developed with HTML and CSS. If you load JavaScript into a page, it usually provides effects and interactions in the form of small fragments. Generally, all JavaScript code is written in a file and loaded into a filescriptTag. […]