design pattern
- Design Patterns] Factory Method Pattern
- Abstract Factory Pattern
- Design Patterns] Single Case Pattern
- Design Patterns] Strategy Pattern
- Design Patterns] Observer Pattern
- Design Patterns] Decoration Patterns
- Design Patterns] Adapter Pattern
I. Introduction
The adapter pattern is a structural design pattern that enables objects with incompatible interfaces to cooperate with each other.
An adapter can act as a wrapper between two objects, taking calls to one object and converting them into a format and interface recognizable by the other.
The adapter pattern is common in PHP code. It is often used by systems that are based on some legacy code. In this case, adapters allow legacy code and modern classes to work together.
Application Scenarios:
(1) Adapter classes can be used when you wish to use a class but its interface is incompatible with other code.
(2) If you need to reuse some classes that are in the same inheritance system and they have some additional methods in common, but these common methods are not common to all subclasses in this inheritance system.
II. Advantages and disadvantages
Pros:
- Single duty principle: You can separate the interface or data transformation code from the main business logic of the program.
- Open-Closed Principle: As long as the client code interacts with the adapter through the client interface, you can add new types of adapters to your program without modifying existing client code.
Drawbacks:
- The overall complexity of the code increases because of the need to add a new set of interfaces and classes.
III. Core structure
Adapter: The main thing is this adapter class, which is used for adapting and extending functions or interfaces.
IV. Code Implementation
1. Application Scenario 1
Adapter classes can be used when you wish to use a class but its interface is not compatible with other code.
For example, there is a computer monitor itself is VGA cable, but now you only have HDMI cable in hand, this time there is only an adapter (adapter) can be used.
(1) LcdInterface class itself is equivalent to the computer display, vga () method is equivalent to the computer display comes with the vga format interface
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/8/1 * Time: 15:47 */ namespace app\admin\service\mode\adapter\other; /** * LcdInterface interface class itself is equivalent to the computer monitor * The vga() method is equivalent to the vga format interface that comes with a computer display. */ interface LcdInterface { public function vga(); }
(2) Adaptors (adapters)
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/8/1 * Time: 15:54 */ namespace app\admin\service\mode\adapter\other; /** * :: Adapter (adapter) */ class Adapter implements LcdInterface { private NewDataCable $chinaPlug; public function __construct(NewDataCable $chinaPlug) { $this->chinaPlug = $chinaPlug; } /** * Converts the HDMI connector to VGA format using an adapter. * Equivalent to the monitor is still using the VGA interface, but through the adapter, the HDMI interface will be converted to VGA interface * @return void * @Author: fengzi * @Date: 2024/10/11 14:20 */ public function vga() { // TODO: Implement plug120V() method. $this->chinaPlug->hdmi(); echo 'Convert HDMI port to VGA format using an adapter' . PHP_EOL; } }
(3) New cable class that supports HDMI interface.
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/8/1 * Time: 15:50 */ namespace app\admin\service\mode\adapter\other; /** * :: New data cable class with HDMI support */ class NewDataCable { public function hdmi() { echo "HDMI port." . PHP_EOL; } }
(4) Calling method
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:29 */ namespace app\admin\controller\mode\adapter; use app\admin\service\mode\adapter\other\Adapter; use app\admin\service\mode\adapter\other\NewDataCable; class AdapterController { /** * An adapter class can be used when you wish to use a class but its interface is incompatible with other code. * @return void * @Author: fengzi * @Date: 2024/10/10 15:41 */ public function change() { $adapter = new Adapter(new NewDataCable());
$adapter->vga();
dd(' The conversion is finished and can be used with the '); } }
(5) Presentation of results
2. Application Scenario 2
If you need to reuse some classes, they are in the same inheritance system and they have some additional methods in common, but these common methods are not common to all the subclasses that are all in this inheritance system.
For example, if you have a class file of the same type that has a method you want to use, but it is not comprehensive. This time you can use an adapter to inherit the old interface class, and then implement the methods that are not there and use them.
(1) Public Calculator interface class
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:30 */ namespace app\admin\service\mode\adapter; /** * :: Interface classes for calculators */ interface Calculator { /** * Addition * @param string $a Additions * @param string $b Additions * @param int|null $scale Decimal digits reserved * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:37 */ public function add(string $a, string $b, ?int $scale=null); /** * Subtraction * @param string $minuend Subtracted number * @param string $subtrahend The subtracted number * @param int|null $scale Number of decimal places to be retained * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:39 */ public function subtract(string $minuend, string $subtrahend, ?int $scale=null); /** * subtraction * @param string $a multiplier * @param string $b multiplier * @param int|null $scale Number of decimal places to be retained * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:41 */ public function multiply(string $a, string $b, ?int $scale=null); /** * division (math.) * @param string $dividend dividend * @param string $divisor divisor (math.) * @param int|null $scale Number of decimal places to be retained * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:44 */ public function divide(string $dividend, string $divisor, int $scale=null); }
(2) Specific implementation of public interface classes
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:31 */ namespace app\admin\service\mode\adapter; /** * :: Simple computational classes */ class SimpleCalculator implements Calculator { /** * Addition * @param string $a Additions * @param string $b Additions * @param int|null $scale Decimal places to be preserved * @return string * @Author: fengzi * @Date: 2024/7/30 11:37 */ public function add(string $a, string $b, int $scale=null): string { // TODO: Implement add() method. return bcadd($a, $b, $scale); } /** * Subtraction * @param string $minuend Subtracted number * @param string $subtrahend The subtracted number * @param int|null $scale Number of Decimal Places to be Retained * @return string * @Author: fengzi * @Date: 2024/7/30 11:39 */ public function subtract(string $minuend, string $subtrahend, ?int $scale=null): string { // TODO: Implement subtract() method. return bcsub($minuend, $subtrahend, $scale); } /** * Multiplication * @param string $a Multiplier * @param string $b Multiplier * @param int|null $scale Decimal places to be preserved * @return string * @Author: fengzi * @Date: 2024/7/30 11:41 */ public function multiply(string $a, string $b, int $scale=null): string { // TODO: Implement multiply() method. return bcmul($a, $b, $scale); } /** * division (math.) * @param string $dividend dividend * @param string $divisor divisor (math.) * @param int|null $scale Number of decimal places to be retained * @return string * @throws \Exception * @Author: fengzi * @Date: 2024/7/30 11:44 */ public function divide(string $dividend, string $divisor, int $scale=null): string { // TODO: Implement divide() method. if ($divisor == 0) { throw new \Exception("The divisor cannot be zero."); } return bcdiv($dividend, $divisor, $scale); } }
(3) Interface classes that extend other methods after inheriting the public interface class
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:36 */ namespace app\admin\service\mode\adapter; /** * :: Advanced computing classes */ interface AdvancedCalculator extends Calculator { /** * exponents * @param mixed $base figure (in a game) * @param mixed $exponent exponents * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:53 */ public function power(mixed $base, mixed $exponent); /** * square root * @param float $number * @return mixed * @Author: fengzi * @Date: 2024/7/30 11:55 */ public function squareRoot(float $number); }
(4) Adapter adapts all methods
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:59 */ namespace app\admin\service\mode\adapter; /** * Adapter */ class CalculatorAdapter implements AdvancedCalculator { private Calculator $simpleCalculator; public function __construct(SimpleCalculator $simpleCalculator) { $this->simpleCalculator = $simpleCalculator; } /** * exponential operation * @param mixed $base * @param mixed $exponent * @return float|int|mixed|object * @Author: fengzi * @Date: 2024/10/11 14:51 */ public function power(mixed $base, mixed $exponent) { // TODO: Implement power() method. return pow($base, $exponent); } /** * square root * @param $number * @return float|mixed * @Author: fengzi * @Date: 2024/10/11 14:52 */ public function squareRoot($number) { // TODO: Implement squareRoot() method. return sqrt($number); } /** * addition * @param string $a * @param string $b * @param int|null $scale * @return mixed|string * @Author: fengzi * @Date: 2024/10/11 14:52 */ public function add(string $a, string $b, ?int $scale = null) { // TODO: Implement add() method. return $this->simpleCalculator->add($a, $b, $scale); } /** * Subtraction * @param string $minuend * @param string $subtrahend * @param int|null $scale * @return mixed|string * @Author: fengzi * @Date: 2024/10/11 14:52 */ public function subtract(string $minuend, string $subtrahend, ?int $scale = null) { // TODO: Implement subtract() method. return $this->simpleCalculator->subtract($minuend, $subtrahend, $scale); } /** * subtraction * @param string $a * @param string $b * @param int|null $scale * @return mixed|string * @Author: fengzi * @Date: 2024/10/11 14:52 */ public function multiply(string $a, string $b, ?int $scale = null) { // TODO: Implement multiply() method. return $this->simpleCalculator->multiply($a, $b, $scale); } /** * division (math.) * @param string $dividend * @param string $divisor * @param int|null $scale * @return mixed|string * @throws \Exception * @Author: fengzi * @Date: 2024/10/11 14:53 */ public function divide(string $dividend, string $divisor, int $scale = null) { // TODO: Implement divide() method. return $this->simpleCalculator->divide($dividend, $divisor, $scale); } }
(5) Adapter invocation method
<?php /** * Created by PhpStorm * Author: fengzi * Date: 2024/7/30 * Time: 11:29 */ namespace app\admin\controller\mode\adapter; use app\admin\service\mode\adapter\CalculatorAdapter; use app\admin\service\mode\adapter\SimpleCalculator; class AdapterController { public function index() { $calculatorAdapter = new CalculatorAdapter(new SimpleCalculator()); $res = $calculatorAdapter->squareRoot(4); dd($res); } }
(6) Presentation of results