Analysis of Template Method Pattern Example of PHP Design Pattern

  • 2021-11-13 00:56:54
  • OfStack

This paper illustrates the template method pattern of PHP design pattern. Share it for your reference, as follows:

Define the framework of the algorithm in 1 operation, and delay 1 steps to subclasses. So that subclasses can redefine some specific steps of an algorithm without changing its structure.

Abstract templates are used in template method patterns, and their methods fall into two categories:

Basic methods, also known as basic operations, are methods implemented by subclasses and are called in template methods. Template method, there can be one or more, 1 is a specific method, that is, a framework, to achieve the basic method of scheduling, complete fixed logic.

Note: In order to prevent malicious operations, 1 template method with final keyword, not allowed to be overridden.

Note: The basic methods in the abstract template should be designed as protected type as much as possible, which conforms to Dimitri's rule, and the properties or methods that do not need to be exposed should not be set as protected type as much as possible. If the implementation class is not necessary, try not to expand the access rights in the parent class.

Advantages of template method pattern

Encapsulate the invariant part and expand the variable part. Extract common part code for easy maintenance. The behavior is controlled by the parent class and implemented by the subclass

Example:


<?php
/**
 *  Model method pattern 
 */
abstract class ACar {
  abstract protected function start();
  abstract protected function engineBoom();
  abstract protected function alarm();
  abstract protected function stop();
  public function run() {
    $this->start();
    $this->engineBoom();
    $this->alarm();
    $this->stop();
  }
}
final class Benz extends ACar {
  protected function start() {
    echo 'Benz start' . PHP_EOL;
  }
  protected function engineBoom() {
    echo 'Benz engine boom' . PHP_EOL;
  }
  protected function alarm() {
    echo 'Benz alarm' . PHP_EOL;
  }
  protected function stop() {
    echo 'Benz stop' . PHP_EOL;
  }
}
final class BMW extends ACar {
  protected function start() {
    echo 'BMW start' . PHP_EOL;
  }
  protected function engineBoom() {
    echo 'BMW engine boom' . PHP_EOL;
  }
  protected function alarm() {
    echo 'BMW alarm' . PHP_EOL;
  }
  protected function stop() {
    echo 'BMW stop' . PHP_EOL;
  }
  //  Modification order 
  public function run() {
    $this->start();
    $this->alarm();
    $this->engineBoom();
    $this->stop();
  }
}
class Client {
  public static function run() {
    $benz = new Benz();
    $benz->run();
    $bmw = new BMW();
    $bmw->run();
  }
}
Client::run();

Run results:

Benz start Benz engine boom Benz alarm Benz stop BMW start BMW alarm BMW engine boom BMW stop

Note: Part of the content is taken from Zen of Design Pattern

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: