第十三天
自從小帥帥被于老大批了之后,心里非常不爽,因?yàn)橛羞@樣的理由:我已經(jīng)做到了你想要的,為什么還得不到肯定。
什么樣的程序員才是優(yōu)秀的?小帥帥帶著這樣的疑問去了解設(shè)計(jì)模式。
盡管他把設(shè)計(jì)模式看了很多遍,甚至連設(shè)計(jì)模式的名字背得滾瓜爛熟,單例模式、抽象工廠模式、建造者模式、工廠模式、原型模式...等。
但是小帥帥還是不知道如何去用,沒辦法,他只好再次去請教于老大,于老大給了一份代碼讓他去看,看看里面用了什么設(shè)計(jì)模式。
什么樣的程序員才是優(yōu)秀的?有人說,優(yōu)秀的程序員是寫出可以閱讀的代碼,而普通的程序員是寫出可以運(yùn)行的代碼。
于老大的代碼如下:
<?phpclass 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); }}
小帥帥看了代碼后再也按耐不住了,這就是傳說中的于老大,還不是抄的我的代碼。。。
于老大要是聽到小帥帥的想法,會(huì)有什么舉動(dòng)呢?
小帥帥沒辦法繼續(xù)去研究神功秘籍。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注