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


    第十八天

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

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

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

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

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

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

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

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

    其实于老大看到 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类里面,代码如下:

    <?php
    class 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个怎么办,又去请教于老大,于老大直接把代码给了小帅帅,于老大的代码为:

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

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

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

  • 相关阅读:
    android 开机启动
    android 获取lanucher 列表
    原创高端影楼人像专业磨皮法教程详解 附PSD源码
    [转]在SQLPLUS启动和停止Oracle数据库
    挑印刷时间最新的地图!
    Eclipse3.2下进行ArcGIS Server 9.2 Java WebADF开发手记 Eclipse使用技巧
    [藏]常用的匹配正则表达式和实例
    [藏]C# 中的常用正则表达式总结
    [转]使用uDig制作geoserver中需要的style
    [转]geoserver与OpenLayers配置入门
  • 原文地址:https://www.cnblogs.com/oshine/p/3939433.html
Copyright © 2020-2023  润新知