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.
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() | +----------------+
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() {} }
ConcreteClass1.php
class ConcreteClass1 extends AbstractClass { protected function step1() { echo "ConcreteClass1: Step 1<br>"; } protected function step2() { echo "ConcreteClass1: Step 2<br>"; } }
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>"; } }
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(); } }
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>"; } }
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>"; } }
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!