PHP design pattern visitor pattern
Visitor pattern is actually a pattern that allows external classes to obtain the objects of each node of the tree structure and operate on each object. It allows us to extend functions without changing the original tree structure, such as Statistics and more.
In this mode, there are several elements that must be present:
1. Specific element object, the location (i.e. node) that the visitor actually wants to visit
2. Stable tree structure , each node is an element object, usually more in combination mode. It provides the actual location that visitors can access (that is, the visitor accesses the instantiation object of a node in a specific attribute structure. );
3. Visitor interface, here defines the visitor's interface method, which is a method that is used in every node; it is used to reference the visitor at the node, so that the visitor can access the current node.
4. The specific implementation of the visitor inherits the visitor interface to implement the interface methods
//定义元素接口 abstract class User { public function getPoint() { return rand();//改数据应该由数据库中读取,这里就直接模拟某个值了、 } //这里的accept方法用于把访问者引入,在这个方法里,$visitor访问者可以通过User类获取需要的数据进而进行相应的操作。 abstract function accept(UserVisitor $visitor); } //定义元素接口 class VipUser extends User { //在这里getPoint()具体实现就由接口中实现了 //在这里就把当前对象传递给了visitor访问者,在访问者类的visitVip方法中就能根据$this获取必要的数据进行相应的操作 public function accept(UserVisitor $vitor) { $vitor->visitVip($this); } } class NormalUser extends User { //同上的getPoint()具体实现就由接口中实现了 //同VipUser类中的accept public function accept(UserVisitor $vitor) { $vitor->visitNormal($this); } } //定义访问者接口 abstract class UserVisitor { //访问者必须要实现的访问不同用户的接口方法 abstract function visitVip(User $user); abstract function visitNormal(User $user); } //积分操作的访问者实现 class PointActVisitor extends UserVisitor { public function visitVip(User $user) { echo 'Vip用户+10分<br/>'; } public function visitNormal(User $user) { echo 'Normal用户+5分<br/>'; } } //用户树形结构 class Users { protected $users; public function addUser(User $user) { $this->users[] = $user; } }
The above introduces the visitor mode of PHP design pattern, 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

AI Hentai Generator
Generate AI Hentai for free.

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

In Docker, the permission problem of the mounting directory can usually be solved by the following method: adding permission-related options when using the -v parameter to specify the mounting directory. You can specify the permissions of the mounted directory by adding: ro or :rw after the mounted directory, indicating read-only and read-write permissions respectively. For example: dockerrun-v/host/path:/container/path:roimage_name Define the USER directive in the Dockerfile to specify the user running in the container to ensure that operations inside the container comply with permission requirements. For example: FROMimage_name#CreateanewuserRUNuseradd-ms/bin/

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 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

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.
