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