适配器很容易理解, 大多数人家庭都有手机转接器, 用来为移动电话充电,这就是一种适配器. 若是只有USB接头, 就没法将移动电话插到标准插座上. 实际上, 必须使用一个适配器, 一端接USB插头, 一端接插座. 固然, 你能够拿出电气工具,改装USB链接头, 或者从新安装插座, 不过这样会带来不少额外的工做, 并且可能会把链接头或插座弄坏. 因此, 最可取的方法就是找一个适配器. 软件开发也是如此.php
class ChildClass extends ParentClass implements ISomeAdapter { }
<?php class DollarCalc { private $dollar; private $product; private $service; public $rate = 1; public function requestCalc($productNow, $serviceNow) { $this->product = $productNow; $this->service = $serviceNow; $this->dollar = $this->product + $this->service; return $this->requestTotal(); } public function requestTotal() { $this->dollar *= $this->rate; return $this->dollar; } }
<?php class EuroCalc { private $euro; private $product; private $service; public $rate = 1; public function requestCalc($productNow, $serviceNow) { $this->product = $productNow; $this->service = $serviceNow; $this->euro = $this->product + $this->service; return $this->requestTotal(); } public function requestTotal() { $this->euro *= $this->rate; return $this->euro; } }
<?php interface ITarget { public function requester(); }
<?php include_once('EuroCalc.php'); include_once('ITarget.php'); class EuroAdapter extends EuroCalc implements ITarget { public function __construct() { $this->requester(); } public function requester() { $this->rate = 0.8111; return $this->rate; } }
<?php include_once('EuroAdapter.php'); include_once('DollarCalc.php'); class Client { public function __construct() { $euro = '€'; echo "区元: $euro" . $this->makeApapterRequest(new EuroAdapter()) . '<br />'; echo "美圆: $: " . $this->makeDollarRequest(new DollarCalc()) . '<br />'; } private function makeApapterRequest(ITarget $req) { return $req->requestCalc(40,50); } private function makeDollarRequest(DollarCalc $req) { return $req->requestCalc(40,50); } } $woker = new Client();
运行结果以下:css
Euros: €72.999 Dollars: $: 90
<?php interface IFormat { public function formatCSS(); public function formatGraphics(); public function horizontalLayout(); }
<?php include_once('IFormat.php'); class Desktop implements IFormat { public function formatCSS() { echo "引用desktop.css<br />"; } public function formatGraphics() { echo "引用desktop.png图片<br />"; } public function horizontalLayout() { echo '桌面:水平布局'; } }
<?php interface IMobileFormat { public function formatCSS(); public function formatGraphics(); public function verticalLayout(); }
<?php include_once('IMobileFormat.php'); class Mobile implements IMobileFormat { public function formatCSS() { echo "引用mobile.css<br />"; } public function formatGraphics() { echo "引用mobile.png图片<br />"; } public function verticalLayout() { echo '移动端:垂直布局'; } }
<?php include_once('IFormat.php'); include_once('Mobile.php'); class MobileAdapter implements IFormat { private $mobile; public function __construct(IMobileFormat $mobileNow) { $this->mobile = $mobileNow; } public function formatCSS() { $this->mobile->formatCSS(); } public function formatGraphics() { $this->mobile->formatGraphics(); } public function horizontalLayout() { $this->mobile->verticalLayout(); } }
<?php include_once('Mobile.php'); include_once('MobileAdapter.php'); 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(); } } $worker = new Client();