php中的設計模式之--策略模式

來源:互聯網
上載者:User

標籤:策略模式   設計模式   php   

<?php/*所謂策略模式是在不同的事件策略模式就是針對相同的行為,在不同的情境中擁有不同的演算法,將這些演算法封裝起來,並且這些演算法是可以互換的,這樣就對客戶隱藏了相應演算法的實現細節,可以很方便的在運行時選擇具體的行為演算法(即策略)。簡單的策略模式: 保安對於學生和老師進考場這個事件,可以分化出不同的策略,學生要查看考試證據,老師允許存取*/interface Strategy{public function handle();   // 執行的策略(function)}/**對學生採取的策略 */class studentStrategy implements Strategy{public function handle(){echo 'check';}}/**對老師採取的策略 */class teacherStrategy implements Strategy{public function handle(){echo 'gogogo';}}// 情形 class TestCheck{private   $strategy;  //設定策略 public function setStrategy(Strategy $strategy){$this->strategy = $strategy;}// 開始策略public function startStrategy(){$this->strategy->handle(); }}$people = 'teacher';switch($people){case 'student' : $strategy = new studentStrategy () ;break;case 'teacher' : $strategy = new teacherStrategy () ;break;}$safePeople = new TestCheck();$safePeople ->setStrategy($strategy);$safePeople ->startStrategy();

php中的設計模式之--策略模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.