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

WBOY
Freigeben: 2016-07-13 10:19:53
Original
1155 Leute haben es durchsucht

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

第十八天

客串:屌丝的坑人表单神器

走过的那些事儿:数据库那点事儿

起点:手把手教你做关键词匹配项目(搜索引擎)---- 第一天

回顾:手把手教你做关键词匹配项目(搜索引擎)---- 第十七天

上回说到小帅帅把代码交给于老大时,于老大批了小帅帅,小帅帅真的感觉受委屈了。

我们回到站在技术总监的角度看问题,技术总监真的要做到监管代码的可读行吗?

我记得很多公司都是提倡利益,利益作为衡量一个技术总监的价值。

技术总监做错了吗?超出他的职责范围了吗?

其实于老大看到 LinklistCharListHandle类里面exec方法出现了三层foreach,就把小帅帅给批了。

好严格的于老大,还是于老大对小帅帅的期望比较高。

小帅帅没办法,他只好把foreach尽量提取出来,小帅帅的版本:

<span>class</span> LinklistCharListHandle <span>extends</span><span> CharListHandle {
    </span><span>public</span> <span>function</span> <span>exec</span><span>(){
        </span><span>$sql</span> = "select word from category_linklist where cid='<span>$this</span>->selectorItem->cid'"<span>;
        </span><span>$linklist</span> = DB::makeArray(<span>$sql</span><span>);
        </span><span>foreach</span>(<span>$linklist</span> <span>as</span> <span>$strWords</span><span>){
            </span><span>$words</span> = <span>explode</span>(",",<span>$strWords</span><span>);

            </span><span>$this</span>->propertiesTransferCharlist(<span>$words</span><span>);

        }
    }

    </span><span>public</span> <span>function</span> propertiesTransferCharlist(<span>$linkWords</span><span>){
        </span><span>$properties</span> = <span>$this</span>->selectorItem-><span>getProperties();
        </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$property</span><span>){

            </span><span>$this</span>->charlist->addCore(<span>$property</span>-><span>value);
            </span><span>if</span>(<span>in_array</span>(<span>$property</span>->value,<span>$linkWords</span><span>)){
                </span><span>$this</span>->addCores(<span>$linkWords</span><span>);
            }
        }
    }

    </span><span>public</span> <span>function</span> addCores(<span>$words</span><span>){
        </span><span>foreach</span>(<span>$words</span> <span>as</span> <span>$char</span><span>){
            </span><span>$this</span>->charlist->addCore(<span>$char</span><span>);
        }
    }
}</span>
Nach dem Login kopieren

小帅帅提取了两个方法,使程序更加能够看懂了。

其实小帅帅的的做法就是使用重构-改善既有代码的设计的技巧之一,Extract Method 提炼函数

Extract Method:将这段代码放进一个独立函数中,并让函数名称解析该函数的用途。

小帅帅很有成就感,继续把代码给于老大的时候,于老大提到了两点。

1. propertiesTransferCharlist为什么要接受个参数。<br />2. addCores到底是那个类的职责范围。<br /><br /><br />
Nach dem Login kopieren

小帅帅第2种他知道怎么做,他把该方法移到Charlist类里面,代码如下:

<?<span>php
</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> addCores(<span>$words</span><span>){
        </span><span>foreach</span>(<span>$words</span> <span>as</span> <span>$char</span><span>){
            </span><span>$this</span>->addCore(<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>
Nach dem Login kopieren

其实小帅帅的这次做法还是使用重构-改善既有代码的设计的技巧之一,Move Method 搬移函数。

Move Method:在该函数最常引用的类中建立一个有着类似行为的新函数。将旧函数变成一个单纯的委托函数,或是将旧函数完全移除。

小帅实在搞不懂第1个怎么办,又去请教于老大,于老大直接把代码给了小帅帅,于老大的代码为:

<?<span>php
</span><span>class</span> LinklistCharListHandle <span>extends</span><span> CharListHandle {
    
    </span><span>private</span> <span>static</span> <span>$linklist</span><span>; 
    
    </span><span>public</span> <span>function</span> <span>exec</span><span>(){
       </span><span>$this</span>-><span>propertiesTransferCharlist();
    }
    
    </span><span>public</span> <span>static</span> <span>function</span> linklist(<span>$cid</span><span>){
        </span><span>if</span>(!<span>isset</span>(self::<span>$linklist</span>) || !<span>isset</span>(self::<span>$linklist</span>[<span>$cid</span>]) || self::<span>$linklist</span>[<span>$cid</span>] == <span>null</span><span>){
            </span><span>$sql</span> = "select word from category_linklist where cid='<span>$cid</span>'"<span>;
            self</span>::<span>$linklist</span>[<span>$cid</span>] = DB::makeArray(<span>$sql</span><span>);
        }
        </span><span>return</span> self::<span>$linklist</span>[<span>$cid</span><span>];
    }

    </span><span>public</span> <span>function</span><span> propertiesTransferCharlist(){
        </span><span>$properties</span> = <span>$this</span>->selectorItem-><span>getProperties();
        </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$property</span><span>){
            
            </span><span>$this</span>->charlist->addCore(<span>$property</span>-><span>value);            
            </span><span>$this</span>->extendCharlist(<span>$property</span>-><span>value);           
        }
    }
    
    </span><span>public</span> <span>function</span> extendCharlist(<span>$char</span><span>){
        </span><span>$linklist</span> = self::linklist(<span>$this</span>->selectorItem-><span>cid);
        </span><span>foreach</span>(<span>$linklist</span> <span>as</span> <span>$strWords</span><span>){
            </span><span>$words</span> = <span>explode</span>(",",<span>$strWords</span><span>);
            </span><span>if</span>(<span>in_array</span>(<span>$char</span>,<span>$words</span><span>)){
                </span><span>$this</span>->charlist->addCores(<span>$words</span><span>);
            }
        }
        
    }    
}</span>
Nach dem Login kopieren

小帅帅看了,发表了一次感叹,原来代码随便改变下,区别这么大,以前为啥从来没有这感觉。

 

小帅帅真希望自己能够独单一面,不用天天去找于老大。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/870655.htmlTechArticle手把手教你做关键词匹配项目(搜索引擎)---- 第十八天,教你做第十八天 第十八天 客串:屌丝的坑人表单神器 走过的那些事儿:数据库...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!