Home > Backend Development > PHP Tutorial > Quick Start: A Brief Introduction to PHP Adapter Pattern_PHP Tutorial

Quick Start: A Brief Introduction to PHP Adapter Pattern_PHP Tutorial

WBOY
Release: 2016-07-15 13:28:32
Original
1026 people have browsed it

There is a lot worth learning about PHP. Here we mainly introduce the PHP adapter mode. Interface changes are a common problem that programmers must (albeit reluctantly) accept and deal with. Program providers modify their code; system libraries are revised; various programming languages ​​and related libraries develop and evolve. One of my children's numerous toys succinctly describes this dilemma: You can't rationalize a person who's out of his place.

Question

How do you avoid the inconvenience caused by API changes of external libraries? If you wrote a library, could you provide a way to allow existing users of your software to upgrade gracefully, even if you have changed your API? How should you change an object's interface to better suit your needs?

Solution

The PHP adapter pattern provides a completely different interface to objects. You can use adapters to implement a common interface across different classes while avoiding the hassle of upgrading and tearing down client code. Consider what will happen when (not hypothetically!) the API of a third-party library changes. In the past, you could just bite the bullet and modify all the client code, and the situation was often not that simple. You might be working on a new project that takes advantage of features provided by a new version of the library, but you already have a number of older applications that interact with previous versions of the library just fine. You won't be able to demonstrate the use of these new features if the upgrade means touching client code from other applications.

Note: Control Body Pattern

PHP Adapter Pattern is the latest example of Control Body Pattern. The structure of an Adapter is similar to a proxy server (Proxy) and a decorator (Decorator), and the difference between them is that the purpose of the adapter (Adapter) is to change the interface of the encapsulated class, the proxy server (Proxy) and the decorator (Decorator) keeps the interface unchanged.

Sample Code

Let’s see how to protect the application from being affected when the API changes. Suppose you've searched hard for the right library and finally found HwLib, a (hypothetically) set of code designed to send messages. The following is the quoted content:

<ol class="dp-xml">
<li class="alt"><span><span>// PHP4  </span></span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>* the HwLib helps programmers everywhere write their first program  </span></li>
<li class=""><span>* @package HelloWorld  </span></li>
<li class="alt"><span>* @version 1  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>class HwLib {  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>* Say “Hello”  </span></li>
<li class=""><span>* @deprec this function is going away in the future  </span></li>
<li class="alt"><span>* @return string  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>function hello() {  </span></li>
<li class=""><span>return ‘Hello ‘;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>* target audience  </span></li>
<li class=""><span>* @return string  </span></li>
<li class="alt"><span>*/  </span></li>
<li class=""><span>function world() {  </span></li>
<li class="alt"><span>return ‘World!’;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

The following is an example of library operation:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">hw</font></span><span> =& new HwLib;  </span></span></li>
<li class="">
<span>echo $hw-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>hello(), $hw-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>world(); </span>
</li>
</ol>
Copy after login

HwLib has complete documentation. In the document, the author has clearly stated that the hello() method will not be supported (or even eliminated) in future versions. Next, it is now assumed that the second version of HwLib has been released. A new greet() method replaces hello(). Here is the new version of the library (comments have been extracted):

<ol class="dp-xml">
<li class="alt"><span><span>// version 2  </span></span></li>
<li class=""><span>class HwLib {  </span></li>
<li class="alt"><span>function greet() {  </span></li>
<li class=""><span>return ‘Greetings and Salutations ‘;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>function world() {  </span></li>
<li class="alt"><span>return ‘World!’;  </span></li>
<li class=""><span>}  </span></li>
</ol>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446448.htmlTechArticleThere is a lot worth learning about PHP. Here we mainly introduce the PHP adapter mode. The change of the interface is a required procedure. common problems that members of the public must (albeit reluctantly) accept and deal with...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template