


Introduction to Design Patterns - Template Method Pattern (php version)
A joke says: How many steps does it take to put an elephant into the refrigerator?
1. Open the refrigerator
2. Stuff the elephant in
3. Close the refrigerator
Similarly, how many steps are needed to put the lion into the refrigerator?
1. Open the refrigerator
2. Stuff the lion in
3. Close the refrigerator
In the above example, did you find that the two methods actually have the same steps, but the specific implementation is slightly different? In other words, these two types of behaviors can share a step template. This can lead to the design pattern to be discussed this time - the template design pattern.
The principle of template design pattern can be represented by the UML class diagram as shown below:
Specific code example:
LockAnimal.php
<?php /** * 将动物锁进冰箱里抽象类 * @author ben * */ abstract class LockAnimal{ /** * 模板方法,使用final关键词防止子类覆盖 */ public final function lock(){ $this->open(); $this->push(); $this->close(); } /** * 打开冰箱 */ abstract function open(); /** * 将动物推进冰箱 */ abstract function push(); /** * 关上冰箱 */ abstract function close(); }
<?php /** * 将狮子锁进冰箱里 * @author ben * */ require_once 'LockAnimal.php'; class LockElephant extends LockAnimal{ /** * (non-PHPdoc) * @see LockAnimal::open() */ public function open(){ echo "in order to lock an elephant into a fridge, you need to open the fridge first<br />"; } /** * (non-PHPdoc) * @see LockAnimal::push() */ public function push(){ echo "i'm pushing the elephant<br />"; } /** * (non-PHPdoc) * @see LockAnimal::close() */ public function close(){ echo "finally, now i can close the fridge<br />"; } }
<?php /** * 将狮子锁进冰箱里 * @author ben * */ require_once 'LockAnimal.php'; class LockLion extends LockAnimal{ /** * (non-PHPdoc) * @see LockAnimal::open() */ public function open(){ echo "in order to lock an lion into a fridge, you need to open the fridge first<br />"; } /** * (non-PHPdoc) * @see LockAnimal::push() */ public function push(){ echo "i'm pushing the lion<br />"; } /** * (non-PHPdoc) * @see LockAnimal::close() */ public function close(){ echo "finally, now i can close the fridge<br />"; } }
Now the question comes, which company has the best excavator technology? Just kidding. Some animals are more ferocious and will not be pushed into the refrigerator obediently. They need to be anesthetized. We need to add a hook to our program to handle this situation. The modified code is as follows:
LockAnimal.php
<?php /** * 将动物锁进冰箱里抽象类 * @author ben * */ abstract class LockAnimal{ /** * 模板方法,使用final关键词防止子类覆盖 */ public final function lock(){ $this->open(); if($this->needAnesthetic()){ $this->anesthetic(); } $this->push(); $this->close(); } /** * 打开冰箱 */ abstract function open(); /** * 是否需要麻醉 */ protected function needAnesthetic(){ return false; } protected function anesthetic(){ echo "anestheticing the animal"; } /** * 将动物推进冰箱 */ abstract function push(); /** * 关上冰箱 */ abstract function close(); }
LockElephant.php
<?php /** * 将狮子锁进冰箱里 * @author ben * */ require_once 'LockAnimal.php'; class LockElephant extends LockAnimal{ /** * (non-PHPdoc) * @see LockAnimal::open() */ public function open(){ echo "in order to lock an elephant into a fridge, you need to open the fridge first<br />"; } /** * (non-PHPdoc) * @see LockAnimal::push() */ public function push(){ echo "i'm pushing the elephant<br />"; } /** * (non-PHPdoc) * @see LockAnimal::close() */ public function close(){ echo "finally, now i can close the fridge<br />"; } }
<?php /** * 将狮子锁进冰箱里 * @author ben * */ require_once 'LockAnimal.php'; class LockLion extends LockAnimal{ /** * (non-PHPdoc) * @see LockAnimal::open() */ public function open(){ echo "in order to lock an lion into a fridge, you need to open the fridge first<br />"; } protected function needAnesthetic(){ return true; } protected function anesthetic(){ echo "anestheticing the lion<br />"; } /** * (non-PHPdoc) * @see LockAnimal::push() */ public function push(){ echo "i'm pushing the lion<br />"; } /** * (non-PHPdoc) * @see LockAnimal::close() */ public function close(){ echo "finally, now i can close the fridge<br />"; } }
<?php require_once 'LockElephant.php'; require_once 'LockLion.php'; $elephant = new LockElephant(); $lion = new LockLion(); $elephant->lock(); $lion->lock();
The official definition of the template method design pattern is: define the skeleton of an algorithm in a method, while deferring some steps to subclasses. Template methods allow subclasses to redefine certain steps in the algorithm without changing the algorithm structure.
The above introduces the introduction to design patterns - template method pattern (php version), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file
