PHP design mode-Adapter mode

Source: Internet
Author: User
Tags ming
Statement: This series of blog reference "Big Talk design mode", author Geoscience.

The adapter mode (sometimes referred to as a wrapper style or wrapper) fits the interface of a class into what the user expects (the core problem that the adapter pattern solves). An adaptation allows classes that are usually not working together because of incompatible interfaces, by wrapping the class's own interface in an existing class.


Class Diagram:

Foreignplayer role: The interface rules within the interface rules for this role are inconsistent, but internal calls to the role's method capabilities.

Internal interface (IPlayer) role: This is an abstract role that gives the internal expected interface rules.

Adapter (Adapter) Role: By wrapping an Adapter object inside, the adapter interface is transformed into a target interface, which is the core role of the adaptor pattern and is the key to the problem that the adapter mode solves.


Code:

 "; } function Defense () {echo "Forward defense
"; }}/** Center * Class Center */class Center implements iplayer{function Attack () {echo ' Center attack
"; } function Defense () {echo ' Center defense
"; }}//--------------to be adapted-----------/** Yao foreign athlete * Class yaoming */class yaoming{function attack () { echo "Yao Ming attack
"; } function Defense () {echo ' Yao ' defense
"; }}//------------Adapter--------------/** Adapter * Class Adapter */class Adapter implements iplayer{private $_player; function __construct () {$this->_player=new yaoming (); } function Attack () {$this->_player-> attack (); } function Defense () {$this->_player-> defense (); }}

Client Test Code:

Header ("Content-type:text/html;charset=utf-8");//------------------------prototype mode test code------------------require_ Once "./adapter/adapter.php"; $player 1=new Forward (); Echo Striker:
"; $player 1->attack (); $player 1->defense (); echo"
"Echo" Yao Ming Played:
"; $yaoming =new Adapter (); $yaoming->attack (); $yaoming->defense ();

Applicable scenarios

1. All the methods to be implemented are specified in the interface.

2. But to have a specific class that implements this interface, only a few of them are used, and other methods are useless.

Precautions

1. The class that acts as an adapter role is an abstract class that implements an existing interface

2. Why use abstract classes:

This class is not instantiated. Instead of acting as an adapter, it provides a common interface for its subclasses, but its subclasses can focus only on where they are interested.

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