Thirteenth day
Ever since Xiao Shuai Shuai was gangbanged by Mr. Yu, I feel very unhappy because there is this reason: I have done what you want, why can’t I get recognition.
What kind of programmer is good? Xiao Shuai Shuai went to understand the design pattern with such questions.
Although he has read design patterns many times, he even knows the names of design patterns by heart, such as singleton pattern, abstract factory pattern, builder pattern, factory pattern, prototype pattern...etc.
But Xiao Shuaishuai still didn’t know how to use it. He had no choice but to ask Boss Yu again. Boss Yu gave him a copy of the code to look at and see what design patterns were used in it.
What kind of programmer is good? Some people say that good programmers write code that can be read, while ordinary programmers write code that can run.
Yu Boss’s code is as follows:
<?<span>php </span><span>class</span><span> SelectorItem { </span><span>private</span> <span>$item</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$item</span><span>){ </span><span>$this</span>->item = <span>$item</span><span>; } </span><span>public</span> <span>function</span> __get(<span>$name</span><span>){ </span><span>if</span>(<span>isset</span>(<span>$this</span>->item-><span>$name</span><span>)){ </span><span>return</span> <span>$this</span>->item-><span>$name</span><span>; } </span><span>return</span> <span>null</span><span>; } </span><span>public</span> <span>static</span> <span>function</span> createFromApi(<span>$num_iid</span><span>){ </span><span>$client</span> = <span>new</span><span> TopClient(); </span><span>$client</span>->appkey = 'xx'<span>; </span><span>$client</span>->secretKey = 'xx'<span>; </span><span>$req</span> = <span>new</span><span> ItemGetRequest(); </span><span>$req</span>->setFields('props_name,property_alias,detail_url,cid,title'<span>); </span><span>$req</span>->setNumIid(<span>$num_iid</span><span>); </span><span>$resp</span> = <span>$client</span>->execute(<span>$req</span><span>); </span><span>if</span>(<span>isset</span>(<span>$resp</span>-><span>code)){ </span><span>#</span><span> error handle</span> <span>throw</span> <span>new</span> <span>Exception</span>(<span>$resp</span>->msg, <span>$resp</span>-><span>code); } </span><span>return</span> <span>new</span> self(<span>$resp</span>-><span>item); } } </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><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><span>abstract</span> <span>class</span><span> CharListHandle { </span><span>protected</span> <span>$charlist</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$charlist</span><span>){ </span><span>$this</span>->charlist = <span>$charlist</span><span>; } </span><span>abstract</span> <span>function</span> <span>exec</span><span>(); } </span><span>class</span> MenCharListHandle <span>extends</span><span> CharListHandle { </span><span>public</span> <span>function</span> <span>exec</span><span>(){ </span><span>$this</span>->charlist->addCore("男装"<span>); </span><span>$this</span>->charlist->addBlacklist("女"<span>); } } </span><span>class</span> WomenCharListHandle <span>extends</span><span> CharListHandle{ </span><span>public</span> <span>function</span> <span>exec</span><span>(){ </span><span>$this</span>->charlist->addCore("女装"<span>); </span><span>$this</span>->charlist->addBlacklist("男"<span>); } } </span><span>#</span><span> 其他CharList Handle小帅帅完成</span> <span>class</span><span> Selector { </span><span>private</span> <span>static</span> <span>$charListHandle</span> = <span>array</span><span>( </span>"男装"=>"MenCharListHandle", "女装"=>"WomenCharListHandle", "情侣装"=>"LoversCharListHandle", "童装"=>"ChildrenCharListHandle"<span> ); </span><span>public</span> <span>static</span> <span>function</span> select(<span>$num_iid</span><span>){ </span><span>$selectorItem</span> = SelectorItem::createFromApi(<span>$num_iid</span><span>); Logger</span>::trace(<span>$selectorItem</span>-><span>props_name); </span><span>$matchTitle</span> = <span>$selectorItem</span>->title.<span>$selectorItem</span>-><span>props_name; </span><span>$charlist</span> = <span>new</span><span> CharList(); </span><span>foreach</span>(self::<span>$charListHandle</span> <span>as</span> <span>$matchKey</span>=><span>$className</span><span>){ </span><span>if</span>(<span>preg_match</span>("/<span>$matchKey</span>/",<span>$matchTitle</span><span>)){ </span><span>$handle</span> = self::createCharListHandle(<span>$className</span>,<span>$charlist</span><span>); </span><span>$handle</span>-><span>exec</span><span>(); } } </span><span>//</span><span>do search things </span> <span> } </span><span>public</span> <span>static</span> <span>function</span> createCharListHandle(<span>$className</span>,<span>$charlist</span><span>){ </span><span>if</span>(<span>class_exists</span>(<span>$className</span><span>)){ </span><span>return</span> <span>new</span> <span>$className</span>(<span>$charlist</span><span>); } </span><span>throw</span> <span>new</span> <span>Exception</span>("class not exists",0<span>); } }</span>
After Xiao Shuaishuai read the code, he couldn’t hold it any longer . This is the legendary Boss Yu, and he didn’t copy my code. . .
What would Boss Yu do if he heard Xiao Shuai Shuai’s thoughts?
Xiao Shuai Shuai can’t continue to study the magic secrets.