Example of adapter pattern code for PHP design mode

Source: Internet
Author: User


Target:


The interface of one class can be converted into another interface that the customer wants, so that the incompatible interface can work together. The popular understanding is to adapt the different interfaces into a unified API interface.


role:


Target, which defines which interface the other classes are converted to, which is our desired interface.


Adaptee is the interface that needs to be adapted.


Adapter adapter, the other two roles are already existing roles, and the adapter role needs to be newly established, it is used to adapt the Adaptee and target interface.
Application Scenarios:


If the data operation has MySQL, Mysqli, PDO, SQLite, PostgreSQL, etc., if the build environment needs to replace the database, you can use the adapter mode unified interface. The same is true of the cache scenario, which is more convenient to replace the caching strategy (Memcache, Redis, APC).


Advantages:


The adapter is used by the adaptor to complete the adaptation target to achieve the purpose of transparent to the customer.


Example code:

From http://www.lai18.com//adaptation target, the specified interface will be implemented by the adaptation object interface idatabase{public  function Connect ($host, $username, $ password, $database);  Public Function query ($sql);} Adapter class Mysql implements idatabase{  protected $connect;   Public function Connect ($host, $username, $password, $database)  {    $connect = mysql_connect ($host, $username, $ password);    mysql_select_db ($database, $connect);    $this->connect = $connect;    //...  }   Public Function Query ($sql)  {/  /...}}//Adapter class Postgresql implements idatabase{  protected $ Connect;   Public function Connect ($host, $username, $password, $database)  {    $this->connect = Pg_connect ("host=$ Host dbname= $database user= $username password= $password ");    //...  }   Public Function Query ($sql)  {    //...  }}  The client uses $client = new Postgresql (); $client->query ($sql);


Reference: http://www.lai18.com/content/409861.html

Example of adapter pattern code for PHP design mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.