“This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Curriculum background

  • I recently completed a project that had a slight understanding of code layering but had problems with architectural design rationality
  • Look at the essence of everythingThe basics are hard and the underlying knowledge is solid so that you can write better code and go further
  • Once again, I’m brushing up on the basics of THINKING about design patterns and PHP as a struggling programmer
  • As the gold digger got a reward, he took notes and shared them.

The text start

So this is where we left off in the last video

2 Create an Observer uniform processing class, Observer, Equipsaveob.php


      
namespace Observer;
/** * Create an abstract class that can only be inherited and not instantiated */
abstract class EquipSaveOb{

    private $observer_arr = []; // Save the observer

    /** * Add Observer * Must be an object of a EquipSaveI interface to be added to an Observer */
    public function addObServer(\Observer\EquipSaveI $server){
        $this->observer_arr[] = $server;
    }

    /** * Perform the operation */
    public function executeAll(){
        foreach ($this->observer_arr as $key= >$value) {
            $value->execute(); }}}Copy the code

Because an abstract class can only be inherited and cannot be created, this insures that any such observer can be used only if he or she is inst/EquipSaveOb.

3 We create the class that performs the action, and then inherit the observer method above


      
namespace Observer;

class EquipSave extends EquipSaveOb{

    /** * Perform the save operation */
    public function save(){
        $this->executeAll(); }}Copy the code

The binding observer here, and the execution method is already written. All we need to do is add observers to the current operation, and the code in each observer will be executed in turn.

4 Create an observer and perform the operation

class Message implements \Observer\EquipSaveI{
    public function execute(){
        echo 'Device station message 

'
; }}class Notify implements \Observer\EquipSaveI{ public function execute(){ echo 'Device SMS notification

'
; }}$res = new \Observer\EquipSave(); $res->addObServer(new Message()); $res->addObServer(new Notify()); $res->save(); Copy the code

Two classes have been added for two operations. When you access the URL, you will see the result of a successful execution.