成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

什么是接口?

piapia / 1780人閱讀

摘要:接口是兩個(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

相關(guān)文章

  • 接口測(cè)試常見(jiàn)問(wèn)題

    摘要:同時(shí)我們也可以使用控件來(lái)實(shí)現(xiàn)小型的接口自動(dòng)化來(lái)提高接口測(cè)試效率。接口測(cè)試的流程首先我們要了解需求,熟悉業(yè)務(wù)場(chǎng)景然后根據(jù)需求文檔,接口文檔以及業(yè)務(wù)場(chǎng)景來(lái)編寫測(cè)試用例。 ...

    edgardeng 評(píng)論0 收藏0
  • 如何形成一個(gè)完整的HTML對(duì)象

    摘要:定義是一個(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í)間,...

    freewolf 評(píng)論0 收藏0
  • 接口測(cè)試要測(cè)試什么?

    摘要:接口測(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ì)用例。 ...

    邱勇 評(píng)論0 收藏0
  • 什么接口測(cè)試?接口測(cè)試基礎(chǔ)、案例及Json格式詳解

    摘要:什么是接口測(cè)試全稱接口是一個(gè)位于復(fù)雜系統(tǒng)之上能簡(jiǎn)化任務(wù),像中間人一樣不需要你了解詳細(xì)的所有細(xì)節(jié)。接口測(cè)試與性能測(cè)試之間存在接口性能測(cè)試,主要通過(guò)來(lái)進(jìn)行壓測(cè)。 很多小...

    WalkerXu 評(píng)論0 收藏0
  • 什么Java Marker Interface(標(biāo)記接口

    摘要:而標(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里的...

    xiaoxiaozi 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<