PHP design mode Adapter Pattern Code Instance

Source: Internet
Author: User
Tags postgresql

This article mainly introduces the PHP design pattern Adapter Pattern code example, this article explained the goal, the role, the application scene, the superiority and so on content, and gives the code example, needs the friend to be possible to refer to under

Goal:

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

Role:

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

Adaptee is adapted to the interface that needs to be fitted.

Adapter adapters, the other two roles are already existing roles, and the adapter role needs to be created to match the adaptee to the target interface.

Application Scenario:

If the data operation has MySQL, Mysqli, PDO, SQLite, PostgreSQL, etc., if the generation environment needs to replace the database, you can use the adapter mode unified interface. Also in the same cache scenario, this would be more convenient to replace the caching policy (memcache, Redis, APC).

Advantage:

The adapter completes the suitability of the matching target through the adaptor, in order to achieve the purpose of transparent use of the customer.

Sample code:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Above

Target matching goal: Idatabase interface

Adaptee: Database operation functions for MySQL and PostgreSQL

Adapter adapters: MySQL class and PostgreSQL class

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: [email protected] 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.