• 手把手教你做关键词匹配项目(搜索引擎)---- 第十三天


    第十三天

    自从小帅帅被于老大批了之后,心里非常不爽,因为有这样的理由:我已经做到了你想要的,为什么还得不到肯定。

    什么样的程序员才是优秀的?小帅帅带着这样的疑问去了解设计模式。

    尽管他把设计模式看了很多遍,甚至连设计模式的名字背得滚瓜烂熟,单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式...等。

    但是小帅帅还是不知道如何去用,没办法,他只好再次去请教于老大,于老大给了一份代码让他去看,看看里面用了什么设计模式。

    什么样的程序员才是优秀的?有人说,优秀的程序员是写出可以阅读的代码,而普通的程序员是写出可以运行的代码。

    于老大的代码如下:

    <?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);
        }
    }

    小帅帅看了代码后再也按耐不住了,这就是传说中的于老大,还不是抄的我的代码。。。

    于老大要是听到小帅帅的想法,会有什么举动呢?

    小帅帅没办法继续去研究神功秘籍。

  • 相关阅读:
    1. Change the emulator screen size
    Dynamic Programming for TSP
    框架的概念及用反射技术开发框架的原理
    【PHP 】 伪静态
    【PHP 】伪静态
    框架-Java:Spring MVC
    开源-解决方案-实时数据追踪:Zipkin 介绍
    报表-类型:瀑布图
    报表:目录
    软件-开发软件-Java-Eclipse:百科
  • 原文地址:https://www.cnblogs.com/oshine/p/3926904.html
Copyright © 2020-2023  润新知