第十三天
自從小帥帥被於老大批了之後,心裡非常不爽,因為有這樣的理由:我已經做到了你想要的,為什麼還得不到肯定。
什麼樣的程序員才是優秀的?小帥帥帶著這樣的疑問去了解設計模式。
盡管他把設計模式看了很多遍,甚至連設計模式的名字背得滾瓜爛熟,單例模式、抽象工廠模式、建造者模式、工廠模式、原型模式...等。
但是小帥帥還是不知道如何去用,沒辦法,他只好再次去請教於老大,於老大給了一份代碼讓他去看,看看裡面用了什麼設計模式。
什麼樣的程序員才是優秀的?有人說,優秀的程序員是寫出可以閱讀的代碼,而普通的程序員是寫出可以運行的代碼。
於老大的代碼如下:
<?php class 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); } }
小帥帥看了代碼後再也按耐不住了,這就是傳說中的於老大,還不是抄的我的代碼。。。
於老大要是聽到小帥帥的想法,會有什麼舉動呢?
小帥帥沒辦法繼續去研究神功秘籍。