·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 手把手教你做关键词匹配项目(搜索引擎)---- 第十八天

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

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
手把手教你做关键词匹配项目(搜索引擎)---- 第十八天

第十八天

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

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

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

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

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

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

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

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

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

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

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

class LinklistCharListHandle extends CharListHandle {    public function exec(){        $sql = "select Word from category_linklist where cid='$this->selectorItem->cid'";        $linklist = DB::makeArray($sql);        foreach($linklist as $strWords){            $words = explode(",",$strWords);            $this->PRopertiesTransferCharlist($words);        }    }    public function propertiesTransferCharlist($linkWords){        $properties = $this->selectorItem->getProperties();        foreach($properties as $property){            $this->charlist->addCore($property->value);            if(in_array($property->value,$linkWords)){                $this->addCores($linkWords);            }        }    }    public function addCores($words){        foreach($words as $char){            $this->charlist->addCore($char);        }    }}

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

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

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

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

1. propertiesTransferCharlist为什么要接受个参数。2. addCores到底是那个类的职责范围。

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

<?phpclass CharList {    private $core = array();    private $blacklist = array();    public function addCore($char){        if(!in_array($char,$this->core))            $this->core[] = $char;    }        public function addCores($words){        foreach($words as $char){            $this->addCore($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;    }}

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

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

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

<?phpclass LinklistCharListHandle extends CharListHandle {        private static $linklist;         public function exec(){       $this->propertiesTransferCharlist();    }        public static function linklist($cid){        if(!isset(self::$linklist) || !isset(self::$linklist[$cid]) || self::$linklist[$cid] == null){            $sql = "select word from category_linklist where cid='$cid'";            self::$linklist[$cid] = DB::makeArray($sql);        }        return self::$linklist[$cid];    }    public function propertiesTransferCharlist(){        $properties = $this->selectorItem->getProperties();        foreach($properties as $property){                        $this->charlist->addCore($property->value);                        $this->extendCharlist($property->value);                   }    }        public function extendCharlist($char){        $linklist = self::linklist($this->selectorItem->cid);        foreach($linklist as $strWords){            $words = explode(",",$strWords);            if(in_array($char,$words)){                $this->charlist->addCores($words);            }        }            }    }

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

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