摘要:工廠模式工廠模式代碼片段訪問(wèn)靜態(tài)屬性要加符靜態(tài)方法生成實(shí)例對(duì)象,作為函數(shù)的參數(shù)工廠就是負(fù)責(zé)生成對(duì)象的類或方法工廠模式,是把創(chuàng)造者類和要生產(chǎn)的類分開,創(chuàng)建者是個(gè)工廠類,定義了用于生產(chǎn)產(chǎn)品對(duì)象的方法行程特殊的代碼重復(fù),不必要的子類話,為了工
/* 工廠模式代碼片段*/ class Employee{ private static $type = array("minion", "wellcon", "student"); public static function recuit($name) { $num = rand(1, count(Employee::$type)) - 1 ; $class = Employee::$type[$num]; // 訪問(wèn)靜態(tài)屬性要加$符 return new $class($name); } } $boss = new Boss(); $boss->addEmployee(Employee::recuit("hard")); // 靜態(tài)方法生成實(shí)例對(duì)象,作為addEmployee函數(shù)的參數(shù)
工廠就是負(fù)責(zé)生成對(duì)象的類或方法
class Demo { public function getInstance($type) { switch($type){ case "1": return new A(); break; case "2": return new B(); break; case "3": return new C(); break; } } }
工廠模式,是把創(chuàng)造者類和要生產(chǎn)的類分開,創(chuàng)建者是個(gè)工廠類,定義了用于生產(chǎn)產(chǎn)品對(duì)象的方法
abstract class commonMessage { abstract function getInstance(); } class A extends commonMessage{ public function getInstance() { return new A(); } } class B extends commonMessage{ public function getInstance() { return new B(); } }
行程特殊的代碼重復(fù),不必要的子類話,為了工廠模式,而為創(chuàng)建類創(chuàng)建子類
對(duì)于那些不必須要的子類模式,合并起來(lái),通過(guò)一個(gè)類中多個(gè)方法就可以完成工廠輸出
abstract class messageFactor { abstract function getHeader(); abstract function getMail(); abstract function getMobile(); abstract function getFooter(); } class smudgeMo extends messageFactor { public function getHeader() { return "Header"; } public function getMail() { return new Mail(); } public function getMobile() { return new Mobile(); } public function getFooter() { return "Footer"; } } class julylovinMo extends messageFactor { // 和上個(gè)子類相似 }
雖然減少了子類繼承,但是耦合問(wèn)題太嚴(yán)重,如果增加一個(gè)產(chǎn)品類型, 抽象函數(shù)和子類繼承體,都需要增加對(duì)應(yīng)的方法
abstract class factory{ const APPT = 1; const TTD = 1; const CONTACT = 1; abstract function make($flag_int); } class mailModel extends factory{ public function make($flag_int) { switch ($flag_int){ case self::APPT: return new apptEncoder(); break; case self::TTD: return new ttdEncoder(); break; case self::CONTACT: return new contactEncoder(); break; } } }
緊湊型工廠模式,但是耦合度高,不利于維護(hù)
全局變量將類捆綁于特定環(huán)境中,破壞了封裝
特點(diǎn):
preference 可以被任何對(duì)象調(diào)用,無(wú)需將對(duì)象作為參數(shù)傳遞
preference 不該保存在全局變量中
系統(tǒng)中不應(yīng)超過(guò)一個(gè)preference對(duì)象,即只允許實(shí)例化一次
class Preference { static $instance; public $name; private function __construct() { } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public static function getInstance() { if (empty(self::$instance)) // 控制了只有一個(gè)Preference實(shí)例對(duì)象 { self::$instance = new Preference(); } return self::$instance; } } $prf = Preference::getInstance(); // 任何地方可以獲取Preference實(shí)例對(duì)象 $prf->setName("Julylovin"); echo $prf->getName(); // Julylovin
用組合代替繼承,抽象工廠模式有平行層次繼承,會(huì)有耦合問(wèn)題
使用clone關(guān)鍵詞復(fù)制已經(jīng)存在的產(chǎn)品, 具體產(chǎn)品本身,便成為自身生成的基礎(chǔ)
class sea { private $navigability = 0 ; //可航行 public function __construct($navigability) { $this->navigability = $navigability; } } class earthsea extends sea{} class plain{} class earthplain extends plain{} class forest{} class earthforest extends forest{} class factory{ private $sea; private $plain; private $forest; public function __construct(sea $sea, plain $plain, forest $forest) { $this->sea = $sea; $this->plain = $plain; $this->forest = $forest; } public function getSea() { return clone $this->sea;// 只是引用對(duì)象,是同一個(gè)對(duì)象,并非兩個(gè)對(duì)象 } public function getPlain() { return clone $this->plain; } public function getForest() { return clone $this->forest; } } $earthInstance = new factory(new earthsea(1), new earthplain(), new earthforest()); //海域可航行 $earthInstance->getForest(); // new earthforest() 的實(shí)例
組合模式有助于集合和組件之間關(guān)系建立模型
槍手(Archer)組合成軍隊(duì)(Arm),多個(gè)槍手可以增加軍隊(duì)的戰(zhàn)斗力(bombardStrength)
abstract class unit { public function addunit(Unit $unit){ // 阻止獨(dú)立單元再次添加對(duì)象 throw new unitException(get_class($unit)."is a leaf"); } public function removeunit(Unit $unit){ // 阻止獨(dú)立單元?jiǎng)h除添加對(duì)象 throw new unitException(get_class($unit)."is a leaf"); } abstract function bombardStrength(); } class unitException extends Exception{} class Archer extends unit { public function bombardStrength() { return 4 ; } } class Army extends unit{ //組合模式體 private $units = array(); public function addunit(Unit $unit) { if(in_array($unit, $this->units, true)) { return ; } $this->units[] = $unit; } public function removeunit(Unit $unit) { $units = array(); foreach ($this->units as $item) { if($item !== $unit) { $units[] = $item; } } $this->units = $units; } public function bombardStrength() { $ret = 0 ; foreach ($this->units as $unit) { $ret += $unit->bombardStrength(); } return $ret; } } $SubArmy = new Army(); $SubArmy->addunit( new Archer()); $SubArmy->addunit( new Archer()); echo $SubArmy->bombardStrength(); // 8
組合模式用戶聚合組件,裝飾模式使用類似功能,改變組件具體功能
繼承是共享父類特性的簡(jiǎn)單方法,所以當(dāng)改變某些特性時(shí),各種硬編碼會(huì)出現(xiàn)在繼承體
導(dǎo)致功能定義依賴于繼承體,導(dǎo)致代碼增多,導(dǎo)致代碼重復(fù)
abstract class tile{ abstract function get_wealth(); } class plains extends tile { private $wealth = 2; public function get_wealth() { return $this->wealth; } } class polluted extends plains { public function get_wealth() { return parent::get_wealth()-2; } } class diamond extends plains{ public function get_wealth() { return parent::get_wealth()+10 ; } } // 上圖組合模式 不容易實(shí)現(xiàn)既是鉆石地段又是污染地段的價(jià)值,常規(guī)做法,會(huì)建立類 diamond_polluted
裝飾模式是使用組合和委托,而不是單純的繼承 abstract class tile { abstract function getwealth(); } class plains extends tile { private $wealth = 2; public function getwealth() { return $this->wealth; } } abstract class tile_decorate extends tile{ protected $tile ; // 保護(hù)屬性,便于子類和當(dāng)前類當(dāng)問(wèn) // 構(gòu)造方法,接收實(shí)例對(duì)象,保存在tile屬性中 public function __construct(tile $tile) { $this->tile = $tile; } } class pollute_decorate extends tile_decorate { public function getwealth() { return $this->tile->getwealth()-4; } } class diamon_decorate extends tile_decorate { public function getwealth() { return $this->tile->getwealth()+2; } } $tile = new plains(); echo $tile->getwealth(); $tile = new diamon_decorate(new plains()); echo $tile->getwealth(); $tile = new pollute_decorate(new diamon_decorate(new plains())); // 組合成一個(gè)整體 echo $tile->getwealth(); // 多個(gè)組合及委托,極具靈活性
多個(gè)裝飾器串聯(lián)起來(lái),形成管道,便于創(chuàng)建過(guò)濾器,核心組件和裝飾漆組合,對(duì)其流程進(jìn)行過(guò)濾緩沖,壓縮 class request_helper{} abstract class process_request{ abstract function process(request_helper $req); } class main_process extends process_request { public function process(request_helper $req) { echo __CLASS__."is processing "; } } abstract class process_decorate extends process_request { protected $process_request; public function __construct(process_request $pro) { $this->process_request = $pro; } } class log_process extends process_decorate { public function process(request_helper $req) { print __CLASS__."is processing log "; $this->process_request->process($req); } } class authenticate extends process_decorate { public function process(request_helper $req) { print __CLASS__."is processing authentication "; $this->process_request->process($req); } } class structure_request extends process_decorate { public function process(request_helper $req) { print __CLASS__."is processing structure "; $this->process_request->process($req); } } $process = new authenticate(new structure_request(new log_process(new main_process()))); $process->process(new request_helper()); // auth->struct->log->main
裝飾模式一定是組合和繼承同時(shí)使用
等同于__call 攔截方法,捕捉不存在的方法自動(dòng)調(diào)用
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/30274.html
小編寫這篇文章的主要目的,給大家進(jìn)行講解關(guān)于Pyhton自動(dòng)化測(cè)試的相關(guān)問(wèn)題,比如如何對(duì)其進(jìn)行持續(xù)集成相關(guān)方面的介紹,關(guān)于Jenkins這個(gè)要怎么去進(jìn)行操作呢?下面就給大家詳細(xì)介紹下。 持續(xù)集成 官方術(shù)語(yǔ): 持續(xù)集成(Continuous Integration),也就是我們經(jīng)常說(shuō)的CI 持續(xù)集成(CI)是一種實(shí)踐,可以讓團(tuán)隊(duì)在持續(xù)的基礎(chǔ)上收到反饋并進(jìn)行改進(jìn),不必等到開發(fā)周期后期才尋找...
此篇文章主要是詳細(xì)介紹了pythonGUI多列輸入文本Text的控制方式,具有非常好的實(shí)用價(jià)值,希望能幫助到大家。如有誤或者未考慮到真正的地區(qū),望鼎力相助 Text的屬性wrap fromtkinterimport* root=Tk() root.geometry('200x300') te=Text(root,height=20,width=15) #將多...
文章內(nèi)容主要是詳細(xì)介紹了pythonmemory_profiler庫(kù)制作器和迭代器cpu占用的時(shí)間分析,文章內(nèi)容緊扣主題進(jìn)行詳盡的基本介紹,感興趣的朋友可以了解一下 不進(jìn)行計(jì)算時(shí),生成器和list空間占用 importtime frommemory_profilerimportprofile profile(precision=4) deflist_fun(): start...
文章主要是詳細(xì)介紹了pythonGUI多做輸入文本Text的控制方式,具有非常好的實(shí)用價(jià)值,希望能幫助到大家。如有誤或者未考慮到真正的地區(qū),望鼎力相助 Text的屬性wrap fromtkinterimport* root=Tk() root.geometry('200x300') te=Text(root,height=20,width=15) #將多做輸...
想要做到就要有更多的學(xué)習(xí),你知道為什么React不把他們?cè)O(shè)為默認(rèn)方法#useEvent是一個(gè)剛剛提案的原生Hook,還處于RFC。現(xiàn)在我們就一起來(lái)討論下 RFC:Request for Comments 提案應(yīng)用的還十分廣泛 我們先看看在沒(méi)有 useEvent 會(huì)出現(xiàn)的情況: functionChat(){ const[text,setText]=useState(''...
閱讀 3517·2021-09-27 13:35
閱讀 3557·2019-08-29 17:09
閱讀 2426·2019-08-26 11:30
閱讀 698·2019-08-26 10:32
閱讀 532·2019-08-26 10:23
閱讀 1194·2019-08-26 10:20
閱讀 3150·2019-08-23 15:26
閱讀 3551·2019-08-23 14:33