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.