Home Backend Development PHP Tutorial PHP-模板(template)形式

PHP-模板(template)形式

Jun 13, 2016 am 11:03 AM
abstract function protected

PHP-模板(template)模式
1.定义:定义一个操作的算法的框架,将一些步骤的实现推迟到子类.
其实意思就是操作的算法的骨架不变,即规定好了第一步应该干什么,第二步干什么,再次干什么,但是不规定具体内容.
2.图示
PHP-模板(template)形式
AbstractClass(抽象类):定义了一到多个的抽象方法,以供具体的子类来实现它们;而且还要实现一个模板方法,来定义一个算法的骨架。
ConcreteClass(具体类):实现父类中的抽象方法以完成算法中与特定子类相关的步骤


在此那超市购物为例子,如某A购物非常有规律,他一般先去书籍区转转,然后再去蔬菜瓜果区看看,再去服装鞋帽区逛逛,最后去家电数码区(这就相当与AbstractClass) 
话说上周六A去了一次超市
先到了书籍区,看到莫言的小说,感到应该随随大众,买了一本>,立刻感到成了文艺青年,和周围人不一样了;
后去了蔬菜瓜果区,看到了最喜欢的橘子。。。。。;
然后到了服装鞋帽区,试了试阿迪王的最新款,买了;
到了家电数码区,看中了某款手机手机,没有钱买。
(这就相当与ConcreteClass)


惯例上代码:
1.抽象类

abstract class AbstractClass{    public function trip(){        $this->bookTrip();        $this->vegetablesTrip();        $this->shoesTrip();        $this->digitalTrip();    }    abstract protected function bookTrip();    abstract protected function vegetablesTrip();    abstract protected function shoesTrip();    abstract protected function digitalTrip();}
Copy after login

2.具体类
class ConcreteClass extends AbstractClass{    protected function bookTrip(){        echo "买>\n";    }    protected function vegetablesTrip(){        echo "买10个橘子\n";    }    protected function shoesTrip(){        echo "买阿迪王\n";    }    protected function digitalTrip(){        echo "没钱没手机\n";    }}
Copy after login

3.client
$instance=new ConcreteClass();$instance->trip();
Copy after login


最后模板模式的本质就是固定算法骨架
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

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.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

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 Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

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

The usage and function of Vue.use function The usage and function of Vue.use function Jul 24, 2023 pm 06:09 PM

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

file_exists() function in PHP file_exists() function in PHP Sep 14, 2023 am 08:29 AM

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

clearstatcache() function in PHP clearstatcache() function in PHP Sep 07, 2023 am 09:33 AM

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(

What is the usage of js function What is the usage of js function Oct 07, 2023 am 11:25 AM

The usage of js function function is: 1. Declare function; 2. Call function; 3. Function parameters; 4. Function return value; 5. Anonymous function; 6. Function as parameter; 7. Function scope; 8. Recursive function.

In Java, can we declare a top-level class as protected or private? In Java, can we declare a top-level class as protected or private? Sep 12, 2023 pm 07:21 PM

No, we cannot declare top-level classes as private or protected. It can be public or default (no modifiers). If there are no modifiers, there should be default access. Syntax //Atoplevelclass publicclassTopLevelClassTest{ //Classbody} If a top-level class is declared as private, the compiler will report an error, prompting "The modifier private is not allowed here." This means that top-level classes cannot be private, the same applies to protected access

See all articles