手把手教你作關鍵詞匹配項目(搜索引擎)---- 第十三天

第十三天php

 

自從小帥帥被於老大批了以後,內心很是不爽,由於有這樣的理由:我已經作到了你想要的,爲何還得不到確定。程序員

什麼樣的程序員纔是優秀的?小帥帥帶着這樣的疑問去了解設計模式。設計模式

儘管他把設計模式看了不少遍,甚至連設計模式的名字背得倒背如流,單例模式、抽象工廠模式、建造者模式、工廠模式、原型模式...等。app

可是小帥帥仍是不知道如何去用,沒辦法,他只好再次去請教於老大,於老大給了一份代碼讓他去看,看看裏面用了什麼設計模式。this

 

什麼樣的程序員纔是優秀的?有人說,優秀的程序員是寫出能夠閱讀的代碼,而普通的程序員是寫出能夠運行的代碼。url

 

於老大的代碼以下:spa

<?php
class SelectorItem {

    private $item;

    public function __construct($item){
        $this->item = $item;
    }

    public function __get($name){
        if(isset($this->item->$name)){
            return $this->item->$name;
        }
        return null;
    }

    public static function createFromApi($num_iid){
        $client = new TopClient();
        $client->appkey = 'xx';
        $client->secretKey = 'xx';

        $req = new ItemGetRequest();
        $req->setFields('props_name,property_alias,detail_url,cid,title');
        $req->setNumIid($num_iid);
        $resp = $client->execute($req);

        if(isset($resp->code)){
            # error handle
            throw new Exception($resp->msg, $resp->code);
        }
        return new self($resp->item);
    }
}


class CharList {

    private $core = array();
    private $blacklist = array();

    public function addCore($char){

        if(!in_array($char,$this->core))
            $this->core[] = $char;
    }
    
    public function getCore(){
        return $this->core;
    }

    public function addBlacklist($char){
        if(!in_array($char,$this->blacklist))
            $this->blacklist[] = $char;
    }
    
    public function getBlacklist(){
        return $this->blacklist;
    }
}

abstract class CharListHandle {
    
    protected $charlist;
    public function __construct($charlist){
        $this->charlist = $charlist;
    }
    
    abstract function exec();
}

class MenCharListHandle extends CharListHandle {
    
    public function exec(){
        $this->charlist->addCore("男裝");
        $this->charlist->addBlacklist("女");
    }
}

class WomenCharListHandle extends CharListHandle{
    public function exec(){
        $this->charlist->addCore("女裝");
        $this->charlist->addBlacklist("男");
    }
}

# 其餘CharList Handle小帥帥完成

class Selector {

    private static  $charListHandle = array(
        "男裝"=>"MenCharListHandle",
        "女裝"=>"WomenCharListHandle",
        "情侶裝"=>"LoversCharListHandle",
        "童裝"=>"ChildrenCharListHandle"
    );

    public static function select($num_iid){
        $selectorItem = SelectorItem::createFromApi($num_iid);
        Logger::trace($selectorItem->props_name);
        $matchTitle = $selectorItem->title.$selectorItem->props_name;
        
        $charlist = new CharList();
        
        foreach(self::$charListHandle as $matchKey=>$className){
            if(preg_match("/$matchKey/",$matchTitle)){
                $handle = self::createCharListHandle($className,$charlist);
                $handle->exec();
            }
        }
        
        //do search things       

    }
    
    public static function createCharListHandle($className,$charlist){
        if(class_exists($className)){
            return new $className($charlist);
        }
        throw new Exception("class not exists",0);
    }
}

小帥帥看了代碼後再也按耐不住了,這就是傳說中的於老大,還不是抄的個人代碼。。。設計

於老大要是聽到小帥帥的想法,會有什麼舉動呢?code

小帥帥沒辦法繼續去研究神功祕籍。blog

相關文章
相關標籤/搜索