Example code sharing of PHP template method pattern

黄舟
Release: 2023-03-06 17:46:01
Original
1411 people have browsed it

Template Method Pattern:

The Template Method pattern defines the steps of an algorithm and allows subclasses to provide implementations for one or more steps. Template method pattern: Define the skeleton of an

algorithm in a method, and defer some steps to subclasses. The template method allows subclasses to Redefine certain steps in the algorithm.

##

'; } abstract class TemplateBase { abstract function step1(); abstract function step2(); abstract function step3(); public function doAction() { $this->step1(); if(!$this->skipStep2()) { $this->step2(); } $this->step3(); } /** * 钩子方法 */ public function skipStep2() { return false; } } class ConcreteTemplate extends TemplateBase { public function step1() { echoLine('This is step 1'); } public function step2() { echoLine('This is step 2'); } public function step3() { echoLine('This is step 3'); } // 用来控制是否跳过某些步骤 public function skipStep2() { return false; } } // test code $ct = new ConcreteTemplate(); $ct->doAction();
Copy after login

The above is the detailed content of Example code sharing of PHP template method pattern. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!