Home > php教程 > php手册 > 手把手教你做关键词匹配项目(搜索引擎)---- 第十三天,教你做第十三天

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

WBOY
Release: 2016-06-13 09:26:44
Original
758 people have browsed it

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

第十三天

 

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

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

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

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

 

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

 

于老大的代码如下:

<?<span>php
</span><span>class</span><span> SelectorItem {

    </span><span>private</span> <span>$item</span><span>;

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

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

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

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

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


</span><span>class</span><span> CharList {

    </span><span>private</span> <span>$core</span> = <span>array</span><span>();
    </span><span>private</span> <span>$blacklist</span> = <span>array</span><span>();

    </span><span>public</span> <span>function</span> addCore(<span>$char</span><span>){

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

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

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

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

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

</span><span>#</span><span> 其他CharList Handle小帅帅完成</span>

<span>class</span><span> Selector {

    </span><span>private</span> <span>static</span>  <span>$charListHandle</span> = <span>array</span><span>(
        </span>"男装"=>"MenCharListHandle",
        "女装"=>"WomenCharListHandle",
        "情侣装"=>"LoversCharListHandle",
        "童装"=>"ChildrenCharListHandle"<span>
    );

    </span><span>public</span> <span>static</span> <span>function</span> select(<span>$num_iid</span><span>){
        </span><span>$selectorItem</span> = SelectorItem::createFromApi(<span>$num_iid</span><span>);
        Logger</span>::trace(<span>$selectorItem</span>-><span>props_name);
        </span><span>$matchTitle</span> = <span>$selectorItem</span>->title.<span>$selectorItem</span>-><span>props_name;
        
        </span><span>$charlist</span> = <span>new</span><span> CharList();
        
        </span><span>foreach</span>(self::<span>$charListHandle</span> <span>as</span> <span>$matchKey</span>=><span>$className</span><span>){
            </span><span>if</span>(<span>preg_match</span>("/<span>$matchKey</span>/",<span>$matchTitle</span><span>)){
                </span><span>$handle</span> = self::createCharListHandle(<span>$className</span>,<span>$charlist</span><span>);
                </span><span>$handle</span>-><span>exec</span><span>();
            }
        }
        
        </span><span>//</span><span>do search things       </span>
<span>
    }
    
    </span><span>public</span> <span>static</span> <span>function</span> createCharListHandle(<span>$className</span>,<span>$charlist</span><span>){
        </span><span>if</span>(<span>class_exists</span>(<span>$className</span><span>)){
            </span><span>return</span> <span>new</span> <span>$className</span>(<span>$charlist</span><span>);
        }
        </span><span>throw</span> <span>new</span> <span>Exception</span>("class not exists",0<span>);
    }
}</span>
Copy after login

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

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template