PHP设计模式之调解者模式的深入解析_php技巧
调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象。在调解者模式下,对象的关系和依赖发生冲突时,我们可以使用调解者在耦合的对象之间协调工作流,依赖可以从同事朝调解者或从调解者向同事建立,这两个方向上的依赖都可以使用AbstractColleague或AbstractMediator中断。
对象不是孤立的,对象之间必须相互协作才能完成任务。虽然调解者模式可以限制对象之间的相互作用,但如果滥用,会致使编写聚合性类变得非常困难。举一个实用的例子,在领域驱动设计(Domain-Driven Design)中的服务就是实体之间的调解者。再举一个PHP相关的例子,Zend_Form装饰和过滤功能实际上可以看作是Zend_Form_Decorator和Zend_Filter实例之间的一个简单调解者,它们都使用Zend_Validate对象进行验证。
当调解者必须监听同事对象的事件时,它通常是作为观察者(Observer)实现的,产生一个黑板(blackboard)对象,一些同事写,另一些同事就读。来自同事的事件被推向调解者,再由调解者将其转发给其它订阅的同事,同事之间不需要相互了解,这个架构成功用于随Zend框架发布的Dojo JavaScript库。这个模式的另一个好处是对象的变化包含在计算方法中,可以通过配置不同的调解者实现这一目标,但实例化相关对象将是一个松散的操作,不同容器和工厂之间的协作关系将是分散的。
参与者:
◆同事(Colleague):重点是它的职责,它只与一个调解者Mediator或AbstractMediator通信。
◆调解者(Mediator):协同多个Colleagues(AbstractColleagues)共同工作。
◆AbstractMediator,AbstractColleague:从这些角色的真实实现解耦的可选接口,可能不止一个AbstractColleague角色。
下面的代码实现了一个表单输入的过滤过程,类似于Zend_Form_Element功能。
/**
* AbstractColleague.
*/
interface Filter
{
public function filter($value);
}
/**
* Colleague. We decide in the implementation phase
* that Colleagues should not know the next Colleague
* in the chain, resorting to a Mediator to link them together.
* This choice succesfully avoids a base abstract class
* for Filters.
* Remember that this is an example: it is not only
* Chain of Responsibility that can be alternatively implemented
* as a Mediator.
*/
class TrimFilter implements Filter
{
public function filter($value)
{
return trim($value);
}
}
/** <BR> * Colleague. <BR> */ <BR> class NullFilter implements Filter <BR> { <BR> public function filter($value) <BR> { <BR> return $value ? $value : ''; <BR> } <BR> } <br><br> /** <BR> * Colleague. <BR> */ <BR> class HtmlEntitiesFilter implements Filter <BR> { <BR> public function filter($value) <BR> { <BR> return htmlentities($value); <BR> } <BR> }<BR>
/** <BR> * The Mediator. We avoid referencing it from ConcreteColleagues <BR> * and so the need for an interface. We leave the implementation <BR> * of a bidirectional channel for the Observer pattern's example. <BR> * This class responsibility is to store the value and coordinate <BR> * filters computation when they have to be applied to the value. <BR> * Filtering responsibilities are obviously a concern of <BR> * the Colleagues, which are Filter implementations. <BR> */ <BR> class InputElement <BR> { <BR> protected $_filters; <BR> protected $_value; <br><br> public function addFilter(Filter $filter) <BR> { <BR> $this->_filters[] = $filter; <BR> return $this; <BR> } <br><br> public function setValue($value) <BR> { <BR> $this->_value = $this->_filter($value); <BR> } <br><br> protected function _filter($value) <BR> { <BR> foreach ($this->_filters as $filter) { <BR> $value = $filter->filter($value); <BR> } <BR> return $value; <BR> } <br><br> public function getValue() <BR> { <BR> return $this->_value; <BR> } <BR> } <br><br> $input = new InputElement(); <BR> $input->addFilter(new NullFilter()) <BR> ->addFilter(new TrimFilter()) <BR> ->addFilter(new HtmlEntitiesFilter()); <BR> $input->setValue(' You should use the <h1>-<h6> tags for your headings.'); <BR> echo $input->getValue(), "\n";<BR>

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
