摘要:接口是兩個(gè)對(duì)象之間的契約,其目的不是讓一個(gè)對(duì)象依賴另一個(gè)對(duì)象,而是依賴一個(gè)對(duì)象的能力。我們不管第三方代碼是如何實(shí)現(xiàn)接口的,只關(guān)心第三方代碼是否實(shí)現(xiàn)了指定的接口。
接口是兩個(gè)PHP對(duì)象之間的契約,其目的不是讓一個(gè)對(duì)象依賴另一個(gè)對(duì)象,而是依賴一個(gè)對(duì)象的能力。
接口把我們的代碼和依賴解耦了,而且允許我們的代碼任何實(shí)現(xiàn)了預(yù)期接口的第三方代碼。我們不管第三方代碼是如何實(shí)現(xiàn)接口的,只關(guān)心第三方代碼是否實(shí)現(xiàn)了指定的接口。
定義一個(gè)DocumentStore類
作用是從不同的源收集文本:可以從遠(yuǎn)程URL讀取HTML,也可以讀取流資源,也可以收集終端的輸出
class DocumentStore { protected $data = []; public function addDocument(Documentable $document) { $key = $document->getId(); $value = $document->getContent(); $this->data[$key] = $value; } public function getDocuments() { return $this->data; } }
定義Documentable接口
interface Documentable { public function getId(); public function getContent(); }
這個(gè)接口的定義表明,實(shí)現(xiàn)Documentable接口的任何對(duì)象必須提供一個(gè)公開(kāi)的getId()和getContent()方法
這么做的用處是,我們可以分開(kāi)定義獲取文檔的類,而且使用十分不同的實(shí)現(xiàn)方法
//使用curl從遠(yuǎn)程URL獲取HTML class HtmlDocument implements Documentable { protected $url; public function __construct($url) { $this->url = $url; } public function getId() { return $this->url; } public function getContent() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 1); $html = curl_exec($ch); curl_close($ch); return $html;
//讀取資源流 class StreamDocument implements Documentable { protected $resource; protected $buffer; public function __construct($resource, $buffer = 4096) { $this->resource = $resource; $this->buffer = $buffer; } public function getId() { return "resource-" . (int)$this->resource; } public function getContent() { $streamContent = ""; rewind($this->resource); while (feof($this->resource) == false) { $streamContent .= fread($this->resource, $this->buffer); } return $streamContent; } }
//獲取終端命令的執(zhí)行結(jié)果 class CommandOutputDocument implements Documentable { protected $command; public function __construct($command) { $this->command = $command; } public function getId() { return $this->command; } public function getContent() { return shell_exec($this->command); } }
如何借助這三種收集文檔的實(shí)現(xiàn)方法使用DocumentStore類呢?
$documentStore = new DocumentStore(); //添加HTML文檔 $htmlDoc = new HtmlDocument("https://php.net"); $documentStore->addDocument($htmlDoc); //添加流文檔 $streamDoc = new StreamDocument("stream.txt", "rb"); $documentStore->addDocument($streamDoc); //添加終端命令文檔 $cmdDoc = new CommandOutputDocument("cat /etc/hosts"); $documentStore->addDocument($cmdDoc); print_r($documentStore->getDocuments());
HtmlDocument、StreamDocument、CommandOutputDocument三個(gè)類沒(méi)任何共同點(diǎn),只是實(shí)現(xiàn)了一個(gè)接口
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/22135.html
摘要:同時(shí)我們也可以使用控件來(lái)實(shí)現(xiàn)小型的接口自動(dòng)化來(lái)提高接口測(cè)試效率。接口測(cè)試的流程首先我們要了解需求,熟悉業(yè)務(wù)場(chǎng)景然后根據(jù)需求文檔,接口文檔以及業(yè)務(wù)場(chǎng)景來(lái)編寫測(cè)試用例。 ...
摘要:定義是一個(gè)由可以接收事件的對(duì)象實(shí)現(xiàn)的接口,并且可以為它們創(chuàng)建偵聽(tīng)器。重點(diǎn)分割線只有通過(guò)上面的繼承關(guān)系,我們得到的元素才是一個(gè)完整的對(duì)象,我們才能為它設(shè)置獲取屬性綁定事件添加樣式類等操作。 寫在前面,本文將同步發(fā)布于Blog、掘金、segmentfault、知乎等處,如果本文對(duì)你有幫助,記得為我得到我的個(gè)人技術(shù)博客項(xiàng)目給個(gè)star哦。 為何寫這篇文章? 你可能做Web開(kāi)發(fā)已經(jīng)有一段時(shí)間,...
摘要:接口測(cè)試主要用于檢測(cè)外部系統(tǒng)與系統(tǒng)之間以及內(nèi)部各個(gè)子系統(tǒng)之間的交互點(diǎn)。二接口測(cè)試用例設(shè)計(jì)接口測(cè)試的用例設(shè)計(jì)是關(guān)鍵,不能只是單純正常請(qǐng)求通過(guò)就算接口測(cè)試過(guò)了,要從業(yè)務(wù)功能性能等上去設(shè)計(jì)用例。 ...
摘要:什么是接口測(cè)試全稱接口是一個(gè)位于復(fù)雜系統(tǒng)之上能簡(jiǎn)化任務(wù),像中間人一樣不需要你了解詳細(xì)的所有細(xì)節(jié)。接口測(cè)試與性能測(cè)試之間存在接口性能測(cè)試,主要通過(guò)來(lái)進(jìn)行壓測(cè)。 很多小...
摘要:而標(biāo)記接口則彌補(bǔ)了這個(gè)功能上的缺失一個(gè)類實(shí)現(xiàn)某個(gè)沒(méi)有任何方法的標(biāo)記接口,實(shí)際上標(biāo)記接口從某種意義上說(shuō)就成為了這個(gè)類的元數(shù)據(jù)之一。運(yùn)行時(shí),通過(guò)編程語(yǔ)言的反射機(jī)制,我們就可以在代碼里拿到這種元數(shù)據(jù)。之前維護(hù)元數(shù)據(jù)的重任就落在標(biāo)記接口上了。 先看看什么是標(biāo)記接口?標(biāo)記接口有時(shí)也叫標(biāo)簽接口(Tag interface),即接口不包含任何方法。在Java里很容易找到標(biāo)記接口的例子,比如JDK里的...
閱讀 3087·2023-04-26 02:04
閱讀 1342·2021-11-04 16:07
閱讀 3815·2021-09-22 15:09
閱讀 739·2019-08-30 15:54
閱讀 1964·2019-08-29 14:11
閱讀 2599·2019-08-26 12:19
閱讀 2332·2019-08-26 12:00
閱讀 838·2019-08-26 10:27