一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP設計模式之適配器模式原理與用法分析

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

2019-10-11 11:14編程人,在天涯 PHP教程

這篇文章主要介紹了PHP設計模式之適配器模式,簡單描述了適配器模式的概念、原理并結合實例形式分析了php類適配器模式與對象適配器模式的具體定義與使用方法,需要的朋友可以參考下

本文實例講述了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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲第五色综合网啪啪 | 高清不卡一区二区 | 女人张开腿 让男人桶个爽 免费观看 | 深夜日韩 | 日产2021免费一二三四区 | 男女啪啪gif | 俄罗斯妈妈235 | 毛片在线网址 | 天天gan| 日产欧产va高清 | 日本在线观看www鲁啊鲁视频 | 国产日韩欧美成人 | 大吊小说 | 久久国产精品二区99 | 亚洲人成影院午夜网站 | 91大片淫黄大片在线天堂 | 国产亚洲精品美女 | 精品精品国产自在久久高清 | babes性欧美30 | 亚洲精品成人在线 | 女人肮脏的交易中文字幕未删减版 | 9191免费永久观看 | 成熟女人50岁一级毛片不卡 | 6080窝窝理论 | 激情涩涩| 免费国产午夜高清在线视频 | 草久久网| 精品国产欧美一区二区三区成人 | 久久最新地址获取 | 欧美四虎影院 | 九草在线视频 | 青春草在线观看视频 | acg火影忍者熟密姬纲手h | 九九99在线视频 | 思思玖玖玖在线精品视频 | 粉嫩极品国产在线观看免费 | 青春草视频在线免费观看 | 亚洲性综合网 | 天天草b | 女人麻豆国产香蕉久久精品 | 免费看60分钟大片视频播放 |