PHP design mode Adapter Pattern code example, PHP design mode _php Tutorial

Source: Internet
Author: User

PHP design mode Adapter Pattern code example, PHP design mode


Goal:

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

Advantage:

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

Example code:

Adaptation target, the specified interface will be implemented 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);

Above

Target adapter: Idatabase interface
Adaptee: Database manipulation functions for MySQL and PostgreSQL
Adapter adapters: MySQL class and PostgreSQL class

http://www.bkjia.com/PHPjc/997907.html www.bkjia.com true http://www.bkjia.com/PHPjc/997907.html techarticle PHP design mode Adapter Mode code example, PHP design mode target: A class interface can be converted into another interface that the customer wants, so that the original incompatible interface can be together ...

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