適配器很容易理解, 大多數(shù)人家庭都有手機轉(zhuǎn)接器, 用來為移動電話充電,這就是一種適配器. 如果只有USB接頭, 就無法將移動電話插到標(biāo)準(zhǔn)插座上. 實際上, 必須使用一個適配器, 一端接USB插頭, 一端接插座. 當(dāng)然, 你可以拿出電氣工具,改裝USB連接頭, 或者重新安裝插座, 不過這樣會帶來很多額外的工作, 而且可能會把連接頭或插座弄壞. 所以, 最可取的方法就是找一個適配器. 軟件開發(fā)也是如此.
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 "區(qū)元: $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();新聞熱點
疑難解答