Home > Backend Development > PHP Tutorial > How to use template method pattern in PHP?

How to use template method pattern in PHP?

PHPz
Release: 2024-06-04 17:15:00
Original
519 people have browsed it

The template method pattern defines the skeleton of the algorithm, and the specific steps are implemented by subclasses, allowing subclasses to customize specific steps without changing the overall structure. This pattern is used to: 1. Define the skeleton of the algorithm. 2. Defer the specific behavior of the algorithm to subclasses. 3. Allow subclasses to customize certain steps of the algorithm without changing the overall structure of the algorithm.

How to use template method pattern in PHP?

Template Method Pattern in PHP

Introduction

The Template Method Pattern is a design pattern that The skeleton of the algorithm is defined, and the specific steps are implemented by subclasses. This allows subclasses to customize specific steps without changing the overall structure of the algorithm.

UML Diagram

+----------------+
| AbstractClass  |
+----------------+
| + templateMethod() |
+----------------+

+----------------+
| ConcreteClass1  |
+----------------+
| + concreteMethod1() |
+----------------+

+----------------+
| ConcreteClass2  |
+----------------+
| + concreteMethod2() |
+----------------+
Copy after login

Code Example

AbstractClass.php

abstract class AbstractClass
{
    public function templateMethod()
    {
        $this->step1();
        $this->step2();
        $this->hookMethod();
    }

    protected abstract function step1();
    protected abstract function step2();
    protected function hookMethod() {}
}
Copy after login

ConcreteClass1.php

class ConcreteClass1 extends AbstractClass
{
    protected function step1()
    {
        echo "ConcreteClass1: Step 1<br>";
    }

    protected function step2()
    {
        echo "ConcreteClass1: Step 2<br>";
    }
}
Copy after login

ConcreteClass2.php

class ConcreteClass2 extends AbstractClass
{
    protected function step1()
    {
        echo "ConcreteClass2: Step 1<br>";
    }

    protected function step2()
    {
        echo "ConcreteClass2: Step 2<br>";
    }

    protected function hookMethod()
    {
        echo "ConcreteClass2: Hook Method<br>";
    }
}
Copy after login

Practical case

Assume we have For a student management system, we need to create two pages: the "Student List" page and the "Student Details" page. The two pages use the same layout, but have different content.

StudentManager.php

class StudentManager
{
    public function showStudentList()
    {
        $students = // 获取学生数据
        $view = new StudentListView();
        $view->setStudents($students);
        $view->render();
    }

    public function showStudentDetail($id)
    {
        $student = // 获取学生数据
        $view = new StudentDetailView();
        $view->setStudent($student);
        $view->render();
    }
}
Copy after login

StudentListView.php

class StudentListView extends AbstractView
{
    private $students;

    public function setStudents($students)
    {
        $this->students = $students;
    }

    public function render()
    {
        $this->showHeader();
        $this->showStudents();
        $this->showFooter();
    }

    protected function showStudents()
    {
        echo "<h1>学生列表</h1>";
        echo "<ul>";
        foreach ($this->students as $student) {
            echo "<li>" . $student->getName() . "</li>";
        }
        echo "</ul>";
    }
}
Copy after login

StudentDetailView.php

class StudentDetailView extends AbstractView
{
    private $student;

    public function setStudent($student)
    {
        $this->student = $student;
    }

    public function render()
    {
        $this->showHeader();
        $this->showStudent();
        $this->showFooter();
    }

    protected function showStudent()
    {
        echo "<h1>学生详情</h1>";
        echo "<p>姓名:" . $this->student->getName() . "</p>";
        echo "<p>年龄:" . $this->student->getAge() . "</p>";
    }
}
Copy after login

The above is the detailed content of How to use template method pattern in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template