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