适配器很容易理解, 大多数人家庭都有手机转接器, 用来为移动电话充电,这就是一种适配器. 如果只有USB接头, 就无法将移动电话插到标准插座上. 实际上, 必须使用一个适配器, 一端接USB插头, 一端接插座. 当然, 你可以拿出电气工具,改装USB连接头, 或者重新安装插座, 不过这样会带来很多额外的工作, 而且可能会把连接头或插座弄坏. 所以, 最可取的方法就是找一个适配器. 软件开发也是如此.
class ChildClass extends ParentClass implements ISomeAdapter{ }<?phpclass 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; }}<?phpclass 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; }}<?phpinterface ITarget{ public function requester();}<?phpinclude_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; }}<?phpinclude_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();Euros: €72.999Dollars: $: 90
<?phpinterface IFormat{ public function formatCSS(); public function formatGraphics(); public function horizontalLayout();}<?phpinclude_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 '桌面:水平布局'; }}<?phpinterface IMobileFormat{ public function formatCSS(); public function formatGraphics(); public function verticalLayout();}<?phpinclude_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 '移动端:垂直布局'; }}<?phpinclude_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(); }}<?phpinclude_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();新闻热点
疑难解答