本文實(shí)例講述了PHP設(shè)計(jì)模式之適配器模式原理與用法。分享給大家供大家參考,具體如下:
一、什么是適配器模式
適配器模式有兩種:類適配器模式和對(duì)象適配器模式。其中類適配器模式使用繼承方式,而對(duì)象適配器模式使用組合方式。由于類適配器模式包含雙重繼承,而PHP并不支持雙重繼承,所以一般都采取結(jié)合繼承和實(shí)現(xiàn)的方式來(lái)模擬雙重繼承,即繼承一個(gè)類,同時(shí)實(shí)現(xiàn)一個(gè)接口。類適配器模式很簡(jiǎn)單,但是與對(duì)象適配器模式相比,類適配器模式的靈活性稍弱。采用類適配器模式時(shí),適配器繼承被適配者并實(shí)現(xiàn)一個(gè)接口;采用對(duì)象適配器模式時(shí),適配器使用被適配者,并實(shí)現(xiàn)一個(gè)接口。
二、什么時(shí)候使用適配器模式
適配器模式的作用就是解決兼容性問(wèn)題,如果需要通過(guò)適配(使用多重繼承或組合)來(lái)結(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
四、對(duì)象適配器模式
以桌面環(huán)境轉(zhuǎn)向移動(dòng)環(huán)境為例:
<?php /** * 對(duì)象適配器模式 * 從桌面環(huán)境轉(zhuǎn)向移動(dòng)環(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() { //桌面水平布局 } } //移動(dòng)布局接口 interface IMobileFormat { public function formatCSS(); public function formatGraphics(); public function verticalLayout(); } //移動(dòng)布局類實(shí)現(xiàn) class Mobile implements IMobileFormat { public function formatCSS() { //調(diào)用移動(dòng)布局CSS文件 } public function formatGraphics() { //調(diào)用圖片 } public function verticalLayout() { //移動(dòng)垂直布局 } } //移動(dòng)布局適配器 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(); ?>
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。