婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP設計模式之適配器模式原理與用法分析

PHP設計模式之適配器模式原理與用法分析

熱門標簽:地圖標注專員怎么樣 四川保險智能外呼系統供應商 遼寧ai電銷機器人價格 房產中介用的是什么外呼系統 上海做外呼線路的通信公司 寧波外呼營銷系統 福建銀行智能外呼系統價格 長沙做地圖標注公司 電話機器人銷售主要負責什么

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

一、什么是適配器模式

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

二、什么時候使用適配器模式

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

三、類適配器模式

以貨幣兌換為例:

?php
/**
*  類適配器模式
*        以貨幣兌換為例
**/
//美元計算類
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;
  }
}
//歐元計算類
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();
}
//歐元適配器實現
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();
?>

運行結果:

Euros: €72.999
Dollars: $90

四、對象適配器模式

以桌面環境轉向移動環境為例:

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

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP設計模式之適配器模式(Adapter)原理與用法詳解
  • php設計模式 Adapter(適配器模式)
  • PHP設計模式之適配器模式代碼實例
  • 學習php設計模式 php實現適配器模式
  • php設計模式之適配器模式原理、用法及注意事項詳解
  • PHP設計模式之適配器模式定義與用法詳解
  • php設計模式之適配器模式實例分析【星際爭霸游戲案例】
  • PHP設計模式(四)原型模式Prototype實例詳解【創建型】
  • PHP設計模式(三)建造者模式Builder實例詳解【創建型】
  • PHP設計模式(一)工廠模式Factory實例詳解【創建型】
  • PHP設計模式概論【概念、分類、原則等】
  • PHP設計模式(五)適配器模式Adapter實例詳解【結構型】

標簽:澳門 佛山 深圳 宜春 延安 工商登記 宿遷 常德

巨人網絡通訊聲明:本文標題《PHP設計模式之適配器模式原理與用法分析》,本文關鍵詞  PHP,設計模式,之,適配器,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP設計模式之適配器模式原理與用法分析》相關的同類信息!
  • 本頁收集關于PHP設計模式之適配器模式原理與用法分析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 南丹县| 扶沟县| 穆棱市| 沂水县| 万荣县| 年辖:市辖区| 介休市| 丰宁| 斗六市| 桐梓县| 南宁市| 太仓市| 嵊州市| 弥渡县| 盈江县| 东至县| 山阳县| 成武县| 景谷| 铜山县| 阳高县| 无棣县| 和顺县| 巢湖市| 长春市| 新巴尔虎左旗| 雷州市| 榆树市| 神木县| 横峰县| 蒙自县| 思南县| 酒泉市| 镇安县| 大名县| 常熟市| 盐城市| 建水县| 蒙阴县| 松滋市| 英超|