Principle and Usage Analysis of Adapter Pattern in PHP Design Pattern

  • 2021-09-24 21:35:29
  • OfStack

This paper illustrates the principle and usage of adapter pattern of PHP design pattern. Share it for your reference, as follows:

1. What is the adapter pattern

There are two adapter patterns: class adapter pattern and object adapter pattern. The class adapter pattern uses inheritance, while the object adapter pattern uses composition. Because the class adapter pattern contains dual inheritance, and PHP does not support dual inheritance, 1 generally adopts the way of combining inheritance and implementation to simulate dual inheritance, that is, inheriting one class and implementing one interface at the same time. The class adapter pattern is simple, but slightly less flexible than the object adapter pattern. When using class adapter mode, the adapter inherits the adapter and implements one interface; When using the object adapter pattern, the adapter uses the adaptee and implements one interface.

2. When to use adapter mode

The adapter pattern is used to address compatibility issues, if you need to combine two incompatible systems through adaptation (using multiple inheritance or composition).

3. Class adapter pattern

Take currency exchange as an example:


<?php
/**
*   Class adapter pattern 
*         Take currency exchange as an example 
**/
// Dollar calculation class 
class DollarCalc
{
  private $dollar;
  private $product;
  private $service;
  public $rate = 1;
  public function requestCalc($product,$service)
  {
    $this->product = $product;
    $this->service = $service;
    $this->dollar = $this->product + $this->service;
    return $this->requestTotal();
  }
  public function requestTotal()
  {
    $this->dollar *= $this->rate;
    return $this->dollar;
  }
}
// Euro calculation class 
class EuroCalc
{
  private $euro;
  private $product;
  private $service;
  public $rate = 1;
  public function requestCalc($product,$service)
  {
    $this->product = $product;
    $this->service = $service;
    $this->euro = $this->product + $this->service;
    return $this->requestTotal();
  }
  public function requestTotal()
  {
    $this->euro *= $this->rate;
    return $this->euro;
  }
}
// Euro adapter interface 
interface ITarget
{
  function requester();
}
// Euro adapter implementation 
class EuroAdapter extends EuroCalc implements ITarget
{
  public function __construct()
  {
    $this->requester();
  }
  function requester()
  {
    $this->rate = .8111;
    return $this->rate;
  }
}
// Customer category 
class Client
{
  private $euroRequest;
  private $dollarRequest;
  public function __construct()
  {
    $this->euroRequest = new EuroAdapter();
    $this->dollarRequest = new DollarCalc();
    $euro = " In fact, in fact, the ;
    echo "Euros: $euro" . $this->makeAdapterRequest($this->euroRequest) . "<br />";
    echo "Dollars: $" . $this->makeDollarRequest($this->dollarRequest);
  }
  private function makeAdapterRequest(ITarget $req)
  {
    return $req->requestCalc(40,50);
  }
  private function makeDollarRequest(DollarCalc $req)
  {
    return $req->requestCalc(40,50);
  }
}
$client = new Client();
?>

Run results:

Euros: 2.999
Dollars: $90

4. Object adapter pattern

Take the transition from desktop environment to mobile environment as an example:


<?php
/**
*   Object adapter pattern 
*          Moving from desktop to mobile 
**/
// Desktop layout interface 
interface IFormat
{
  public function formatCSS();
  public function formatGraphics();
  public function horizontalLayout();
}
// Desktop layout class implementation 
class Desktop implements IFormat
{
  public function formatCSS()
  {
    // Calling desktop layout CSS Documents 
  }
  public function formatGraphics()
  {
    // Invoke pictures 
  }
  public function horizontalLayout()
  {
    // Desktop horizontal layout 
  }
}
// Mobile layout interface 
interface IMobileFormat
{
  public function formatCSS();
  public function formatGraphics();
  public function verticalLayout();
}
// Mobile layout class implementation 
class Mobile implements IMobileFormat
{
  public function formatCSS()
  {
    // Invoke mobile layout CSS Documents 
  }
  public function formatGraphics()
  {
    // Invoke pictures 
  }
  public function verticalLayout()
  {
    // Move vertical layout 
  }
}
// Mobile layout adapter 
class MobileAdapter implements IFormat
{
  private $mobile;
  public function __construct(IMobileFormat $mobile)
  {
    $this->mobile = $mobile;
  }
  public function formatCSS()
  {
    $this->mobile->formatCSS();
  }
  public function formatGraphics()
  {
    $this->mobile->formatGraphics();
  }
  public function horizontalLayout()
  {
    $this->mobile->verticalLayout();
  }
}
// Customer category 
class Client
{
  private $mobile;
  private $mobileAdapter;
  public function __construct()
  {
    $this->mobile = new Mobile();
    $this->mobileAdapter = new MobileAdapter($this->mobile);
    $this->mobileAdapter->formatCSS();
    $this->mobileAdapter->formatGraphics();
    $this->mobileAdapter->horizontalLayout();
  }
}
$client = new Client();
?>

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: