欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP設(shè)計(jì)模式之適配器模式原理與用法分析

 更新時(shí)間:2018年04月25日 11:15:29   作者:編程人,在天涯  
這篇文章主要介紹了PHP設(shè)計(jì)模式之適配器模式,簡單描述了適配器模式的概念、原理并結(jié)合實(shí)例形式分析了php類適配器模式與對象適配器模式的具體定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了PHP設(shè)計(jì)模式之適配器模式原理與用法。分享給大家供大家參考,具體如下:

一、什么是適配器模式

適配器模式有兩種:類適配器模式和對象適配器模式。其中類適配器模式使用繼承方式,而對象適配器模式使用組合方式。由于類適配器模式包含雙重繼承,而PHP并不支持雙重繼承,所以一般都采取結(jié)合繼承和實(shí)現(xiàn)的方式來模擬雙重繼承,即繼承一個(gè)類,同時(shí)實(shí)現(xiàn)一個(gè)接口。類適配器模式很簡單,但是與對象適配器模式相比,類適配器模式的靈活性稍弱。采用類適配器模式時(shí),適配器繼承被適配者并實(shí)現(xiàn)一個(gè)接口;采用對象適配器模式時(shí),適配器使用被適配者,并實(shí)現(xiàn)一個(gè)接口。

二、什么時(shí)候使用適配器模式

適配器模式的作用就是解決兼容性問題,如果需要通過適配(使用多重繼承或組合)來結(jié)合兩個(gè)不兼容的系統(tǒng),那就使用適配器模式。

三、類適配器模式

以貨幣兌換為例:

<?php
/**
*  類適配器模式
*        以貨幣兌換為例
**/
//美元計(jì)算類
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;
  }
}
//歐元計(jì)算類
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;
  }
}
//歐元適配器接口
interface ITarget
{
  function requester();
}
//歐元適配器實(shí)現(xiàn)
class EuroAdapter extends EuroCalc implements ITarget
{
  public function __construct()
  {
    $this->requester();
  }
  function requester()
  {
    $this->rate = .8111;
    return $this->rate;
  }
}
//客戶類
class Client
{
  private $euroRequest;
  private $dollarRequest;
  public function __construct()
  {
    $this->euroRequest = new EuroAdapter();
    $this->dollarRequest = new DollarCalc();
    $euro = "€";
    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();
?>

運(yùn)行結(jié)果:

Euros: €72.999
Dollars: $90

四、對象適配器模式

以桌面環(huán)境轉(zhuǎn)向移動環(huán)境為例:

<?php
/**
*  對象適配器模式
*         從桌面環(huán)境轉(zhuǎn)向移動環(huán)境
**/
//桌面布局接口
interface IFormat
{
  public function formatCSS();
  public function formatGraphics();
  public function horizontalLayout();
}
//桌面布局類實(shí)現(xiàn)
class Desktop implements IFormat
{
  public function formatCSS()
  {
    //調(diào)用桌面布局CSS文件
  }
  public function formatGraphics()
  {
    //調(diào)用圖片
  }
  public function horizontalLayout()
  {
    //桌面水平布局
  }
}
//移動布局接口
interface IMobileFormat
{
  public function formatCSS();
  public function formatGraphics();
  public function verticalLayout();
}
//移動布局類實(shí)現(xiàn)
class Mobile implements IMobileFormat
{
  public function formatCSS()
  {
    //調(diào)用移動布局CSS文件
  }
  public function formatGraphics()
  {
    //調(diào)用圖片
  }
  public function verticalLayout()
  {
    //移動垂直布局
  }
}
//移動布局適配器
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();
  }
}
//客戶類
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();
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論