PHP中用接口、抽象類、普通基類實現(xiàn)“面向接口編程”與“耦合方法”簡述
更新時間:2011年03月23日 22:27:24 作者:
邊學(xué)邊做的,為方便自己翻閱而發(fā)布,更為得到高人指點而發(fā)布,歡迎高手指點
復(fù)制代碼 代碼如下:
<?php
/*
邊學(xué)邊做的,為方便自己翻閱而發(fā)布,更為得到高人指點而發(fā)布,歡迎高手指點......
【提示】本例通過測試無誤
【情景設(shè)計】
模擬計算機(jī)主板IDE接口,比如:主板可以存取的儲存器常常有光驅(qū)、硬盤、閃存等等,
為了方便,有必要對這些不同的儲存器設(shè)定同一的接口。
本例還假設(shè)一種前所未有的、存取方式與眾不同的外星儲存器也要加到主板上進(jìn)行存取,
于是需要采用耦合的設(shè)計模式。
【本例主要講述】
1、通過接口、抽象類、一般類繼承三種方式達(dá)到所謂的“接口”模式,以此說明:
A、子類對象可以當(dāng)父類對象用,因為子類是特殊的父類!
B、請注意這三種實現(xiàn)方式中接口、抽象類和一般類繼承的基類寫法!
2、接口模式真的是一紙契約!
3、程序設(shè)計之"耦合設(shè)計模式"!
*/
//----------------------------------------------------------------------
/*【方式一】接口實現(xiàn)方式:*/
interface readandwrite{
function read();
function write();
}
class motherboard{
private $storage;
function __construct(readandwrite $obj){
$this->storage=$obj;
}
function read(){
$this->storage->read();
}
function write(){
$this->storage->write();
}
}
class flash implements readandwrite{
function __construct(){
echo "我是閃存:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class yingpan implements readandwrite{
function __construct(){
echo "我是硬盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class disco implements readandwrite{
function __construct(){
echo "我是光盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
//----------------------------------------------------------------------
/*【方式二】抽象類實現(xiàn)方式:
abstract class readandwrite{
abstract function read();
abstract function write();
}
class motherboard{
private $storage;
function __construct(readandwrite $obj){
$this->storage=$obj;
}
function read(){
$this->storage->read();
}
function write(){
$this->storage->write();
}
}
class flash extends readandwrite{
function __construct(){
echo "我是閃存:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class yingpan extends readandwrite{
function __construct(){
echo "我是硬盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class disco extends readandwrite{
function __construct(){
echo "我是光盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
*/
//----------------------------------------------------------------------
//【方式三】一般類繼承實現(xiàn)方式:
/*
class readandwrite{
function read(){
echo "reading..............";
}
function write(){
echo "writing..............";
}
}
class motherboard{
private $storage;
function __construct(readandwrite $obj){
$this->storage=$obj;
}
function read(){
$this->storage->read();
}
function write(){
$this->storage->write();
}
}
class flash extends readandwrite{
function __construct(){
echo "我是閃存:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class yingpan extends readandwrite{
function __construct(){
echo "我是硬盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
class disco extends readandwrite{
function __construct(){
echo "我是光盤:<br>";
}
function read(){
echo "開始讀取數(shù)據(jù)......<br>";
}
function write(){
echo "開始儲存數(shù)據(jù)......<hr>";
}
}
*/
//----------------------------------------------------------------------
/*
【耦合模式】
耦合模式就是將不同標(biāo)準(zhǔn)的兩個類(本例的接口、抽象類、普通基類與外星儲存器有不同的存取方法),
通過中間轉(zhuǎn)換器,達(dá)到同一標(biāo)準(zhǔn)的目的,就像轉(zhuǎn)接線一樣——本例就是將unknow類的Rdata、Wdata方法轉(zhuǎn)
換成read、write方法,達(dá)到和本例的接口、抽象類、普通基類相同的存取方法, 本例的中間轉(zhuǎn)換器是
Apdater類。
由于php中只能繼承一個類但可以繼承多個接口,所以產(chǎn)生了三種耦合方法:
方法一:中間轉(zhuǎn)換器Apdater類繼承抽象類或普通基類,但由于php中只能繼承一個類,所以在Apdater
類中定義一個外星存儲器類unknow的對象,并用重載繼承的抽象類或普通基類的存取方法的方式來轉(zhuǎn)
換存取方法,達(dá)到同一存取方法的目的。
方法二:中間轉(zhuǎn)換器Apdater類繼承外星存儲器類unknow、接口,此時可以直接用Apdater類的存取方法
(parent::Rdata()與parent::Wdata()——php中子類調(diào)用父類方法的方式),和實現(xiàn)接口規(guī)定方法,
來轉(zhuǎn)換存取方法,達(dá)到同一存取方法的目的。
方法三:與方法一雷同,只是改為繼承(實現(xiàn))接口;
*/
//----------------------------------------------------------------------
/*
【方法一】
*/
/*
class unknow{
function __construct(){
echo "<font color=#ff0000>我是地球人未知的外星儲存器,我有不同于地球儲存器的存取方式:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
class Adpater extends readandwrite{
private $obj;
function __construct(unknow $x){
$this->obj=$x;
}
function read(){
$this->obj->Rdata();
}
function write(){
$this->obj->Wdata();
}
}
*/
//----------------------------------------------------------------------
/*
【方法二】
class unknow{
function __construct(){
echo "<font color=#ff0000>我是地球人未知的外星儲存器,我有不同于地球儲存器的存取方式:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
class Adpater extends unknow implements readandwrite{
function read(){
parent::Rdata();
}
function write(){
parent::Wdata();
}
}
*/
//------------------------------------------------------------------------
/*
【方法三】
*/
class unknow{
function __construct(){
echo "<font color=#ff0000>我是地球人未知的外星儲存器,我有不同于地球儲存器的存取方式:</font><br>";
}
function Rdata(){
echo "I'm reading now......<br>";
}
function Wdata(){
echo "I'm writing now......<hr>";
}
}
class Adpater implements readandwrite{
private $obj;
function __construct(unknow $x){
$this->obj=$x;
}
function read(){
$this->obj->Rdata();
}
function write(){
$this->obj->Wdata();
}
}
//【程序主體調(diào)用】
echo "<strong><font color=#990000 size=20px>面向?qū)ο蟪绦蛟O(shè)計——接口</font></strong><hr>";
$storage1=new flash();
$computer=new motherboard($storage1);
$computer->read();
$computer->write();
$storage2=new yingpan();
$computer=new motherboard($storage2);
$computer->read();
$computer->write();
$storage3=new disco();
$computer=new motherboard($storage3);
$computer->read();
$computer->write();
$un_storage=new unknow();
$apdaterx=new Adpater($un_storage);
$computer=new motherboard($apdaterx);
$computer->read();
$computer->write();
?>
相關(guān)文章
詳談php ip2long 出現(xiàn)負(fù)數(shù)的原因及解決方法
下面小編就為大家?guī)硪黄斦刾hp ip2long 出現(xiàn)負(fù)數(shù)的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
數(shù)據(jù)結(jié)構(gòu)之利用PHP實現(xiàn)二分搜索樹
這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)之利用PHP實現(xiàn)二分搜索樹的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2020-10-10
php array_map與array_walk比較案例詳解
這篇文章主要介紹了php array_map與array_walk比較案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
2021-09-09 
