PHP设计模式漫谈之命令模式_PHP
PHP设计模式漫谈我们已经连载了四期,我们知道了代理模式、责任链模式和结构模式;今天我们一起来讨论的是命令模式(Command),命令模式是封装一个通用操作的机制。
如果你熟悉C或PHP,你可能已经遇到过Command,它相当于程序中的:回调(callback)。回调通常使用一个函数指针或数据结构如PHP中的字符串和数组实现,Command是在一个方法调用之上的抽象,它吸收了所有面向对象的好处:合成、继承和处理。
例如,《设计模式》一书推荐使用Command存储用户行为链,以支持撤销和重做操作。
注意PHP 5.3函数编程能力(闭包)可以被当做Command模式的一个本地实现,但为每一个命令层次结构使用抽象数据类型有助于类型安全。
PHP设计模式中的命令模式
在这个模式中,Invoker(调用者)知道传递给它的Command,无需依赖于真实的ConcreteCommand(具体的命令)实现,解决了通过配置进行方法调用相关的问题,如UI控件按钮和菜单等引用一个Command,它们的行为是通过通用的ConcreteCommand实例呈现的。
参与者:
◆Command(命令):在一个方法调用之上定义一个抽象;
◆ConcreteCommand(具体的命令):一个操作的实现;
◆Invoker(调用者):引用Command实例作为它可用的操作。
下面的代码展示了Validator组件作为Command对象实现的示例:
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="comment"><font color="#008200">/** </font></span> </li> <li class="alt"><span><span class="comment"><font color="#008200"> * The Command abstraction. </font></span> </span></li> <li><span><span class="comment"><font color="#008200"> * In this case the implementation must return a result, </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> * sometimes it only has side effects. </font></span> </span></li> <li><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li class="alt"> <span class="keyword"><strong><font color="#006699">interface</font></strong></span><span> Validator </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="comment"><font color="#008200">/** </font></span> </li> <li><span><span class="comment"><font color="#008200"> * The method could have any parameters. </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> * @param mixed </font></span> </span></li> <li><span><span class="comment"><font color="#008200"> * @return boolean </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li> <span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> isValid(</span><span class="vars"><font color="#dd0000">$value</font></span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span class="comment"><font color="#008200">/** </font></span> </li> <li><span><span class="comment"><font color="#008200"> * ConcreteCommand. </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li> <span class="keyword"><strong><font color="#006699">class</font></strong></span><span> MoreThanZeroValidator </span><span class="keyword"><strong><font color="#006699">implements</font></strong></span><span> Validator </span> </li> <li class="alt"><span>{ </span></li> <li> <span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> isValid(</span><span class="vars"><font color="#dd0000">$value</font></span><span>) </span> </li> <li class="alt"><span> { </span></li> <li> <span> </span><span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$value</font></span><span> > 0; </span> </li> <li class="alt"><span> } </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span class="comment"><font color="#008200">/** </font></span> </li> <li class="alt"><span><span class="comment"><font color="#008200"> * ConcreteCommand. </font></span> </span></li> <li><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li class="alt"> <span class="keyword"><strong><font color="#006699">class</font></strong></span><span> EvenValidator </span><span class="keyword"><strong><font color="#006699">implements</font></strong></span><span> Validator </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> isValid(</span><span class="vars"><font color="#dd0000">$value</font></span><span>) </span> </li> <li><span> { </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$value</font></span><span> % 2 == 0; </span> </li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span class="comment"><font color="#008200">/** </font></span> </li> <li><span><span class="comment"><font color="#008200"> * The Invoker. An implementation could store more than one </font></span> </span></li> <li class="alt"><span><span class="comment"><font color="#008200"> * Validator if needed. </font></span> </span></li> <li><span><span class="comment"><font color="#008200"> */</font></span><span> </span></span></li> <li class="alt"> <span class="keyword"><strong><font color="#006699">class</font></strong></span><span> ArrayProcessor </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">protected</font></strong></span><span> </span><span class="vars"><font color="#dd0000">PHP设计模式漫谈我们已经连载了四期,我们知道了代理模式、责任链模式和<font color="#0000ff">结构模式</font>;今天我们一起来讨论的是命令模式(Command),命令模式是封装一个通用操作的机制。 <p>如果你熟悉C或PHP,你可能已经遇到过Command,它相当于程序中的:回调(callback)。回调通常使用一个函数指针或数据结构如PHP中的字符串和数组实现,Command是在一个方法调用之上的抽象,它吸收了所有面向对象的好处:合成、继承和处理。</p> <p>例如,《设计模式》一书推荐使用Command存储用户行为链,以支持撤销和重做操作。</p> <p>注意PHP 5.3函数编程能力(闭包)可以被当做Command模式的一个本地实现,但为每一个命令层次结构使用抽象数据类型有助于类型安全。</p> <p style="TEXT-ALIGN: center"><img class="fit-image lazy" src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/100413/115P52291-0.jpg" onmousewheel="javascript:return big(this)" style="max-width:90%" style="max-width:90%" alt="PHP设计模式中的命令模式" style="max-width:90%" onload="javascript:if(this.width>498)this.style.width=498;" border="0"> <br><strong><span style="FONT-SIZE: smaller"><font size="2">PHP设计模式中的命令模式</font></span></strong></p> <p>在这个模式中,Invoker(调用者)知道传递给它的Command,无需依赖于真实的ConcreteCommand(具体的命令)实现,解决了通过配置进行方法调用相关的问题,如UI控件按钮和菜单等引用一个Command,它们的行为是通过通用的ConcreteCommand实例呈现的。</p> <p><strong>参与者:</strong></p> <p>◆Command(命令):在一个方法调用之上定义一个抽象;</p> <p>◆ConcreteCommand(具体的命令):一个操作的实现;</p> <p>◆Invoker(调用者):引用Command实例作为它可用的操作。</p> <p>下面的代码展示了Validator组件作为Command对象实现的示例:</p> <pre class="brush:php;toolbar:false">___FCKpd___0
使用PHP设计模式中的命令模式的一些注意事项:
◆方法调用中的某些参数可以在构造ConcreteCommand时提供,有效地局部套用(currying)原始函数;
◆一个Command可以被看作是一个非常简单的只有一个方法的策略(Strategy),重点放在对象的操作上;
◆ConcreteCommands也要组织它们需要的每一个资源,以实现它们的目标,主要是行为的Receiver(接受者),它们调用方法执行一个Command;
◆复合模式,装饰模式和其它模式都可以和命令模式组合,获得更多的Command,装饰Command等等。
rule;使用PHP设计模式中的命令模式的一些注意事项:
◆方法调用中的某些参数可以在构造ConcreteCommand时提供,有效地局部套用(currying)原始函数;
◆一个Command可以被看作是一个非常简单的只有一个方法的策略(Strategy),重点放在对象的操作上;
◆ConcreteCommands也要组织它们需要的每一个资源,以实现它们的目标,主要是行为的Receiver(接受者),它们调用方法执行一个Command;
◆复合模式,装饰模式和其它模式都可以和命令模式组合,获得更多的Command,装饰Command等等。

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

What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

Widgets are a new feature of the Win11 system. They are turned on by default. However, it is inevitable that some users do not use widgets very much and want to disable them because they take up space. So how should they do this? The editor below will teach you how to operate it, and you can try it out. What are widgets? Widgets are small cards that display dynamic content from your favorite apps and services on your Windows desktop. They appear on the widget board, where you can discover, pin, unpin, arrange, resize, and customize widgets to reflect your interests. The widget board is optimized to display relevant widgets and personalized content based on usage. Open the widget panel from the left corner of the taskbar, where you can see live weather

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features
