PHP設(shè)計(jì)模式的策略,適配器和觀察者模式詳解
策略模式
特點(diǎn)
定義一系列算法封裝起來, 讓他們可以相互替代,策略模式提供了管理相關(guān)算法族的辦法, 提供了可以體會(huì)繼承關(guān)系的棒法, 避免使用多重條件轉(zhuǎn)移語句
實(shí)現(xiàn)
<?php
abstract class Strategy
{
abstract function goSchool();
}
class Run extends Strategy
{
public function goSchool() {
echo "走路去學(xué)校";
}
}
class Subway extends Strategy
{
public function goSchool() {
echo "地鐵去學(xué)校";
}
}
class Bike extends Strategy
{
public function goSchool() {
echo "公交去學(xué)校";
}
}
class GoSchoolContext
{
protected $_stratege;
public function __construct($stratege) {
$this->_stratege = $stratege;
}
public function goSchool()
{
$this->_stratege->goSchool();
}
}
$traget = new Run();
$obj = new GoSchoolContext($traget);
$obj->goSchool();
適配器模式
特點(diǎn)
需要的東西在面前,但卻不能用,而短時(shí)間又無法改造它,于是就想辦法適配
實(shí)現(xiàn)
// 適配器
interface Charget
{
public function putCharget();
}
class China implements Charget
{
private $v = 220;
public function putCharget()
{
return $this->v;
}
}
class Adper extends China
{
public function putCharget() {
return parent::putCharget() / 2 + 10;
}
}
class Phone
{
public function charge(Charget $charge)
{
if ($charge->putCharget() != "120") {
echo "不能充電";
} else {
echo "能充電";
}
}
}
$china = new China();
$adper = new Adper();
$phone = new Phone();
$phone->charge($adper);
觀察者模式
特點(diǎn)
當(dāng)一個(gè)對(duì)象狀態(tài)發(fā)生變化時(shí), 依賴他的對(duì)象全部收到通知, 并主動(dòng)更新。觀察者模式實(shí)現(xiàn)了低耦合, 非侵入式的通知與更新機(jī)制。
實(shí)現(xiàn)
<?php
// 主題接口
interface Subject
{
public function register(Observer $observer);
}
// 觀察者接口
interface Observer
{
public function watch();
}
// 主題
class WatchAction implements Subject
{
public $_observers = [];
public function register(\Observer $observer) {
$this->_observers[] = $observer;
}
public function notify()
{
foreach($this->_observers as $object) {
$object->watch();
}
}
}
// 觀察者
class Cat1 implements Observer{
public function watch(){
echo "Cat1 watches TV<hr/>";
}
}
class Dog1 implements Observer{
public function watch(){
echo "Dog1 watches TV<hr/>";
}
}
class People implements Observer{
public function watch(){
echo "People watches TV<hr/>";
}
}
$action = new WatchAction();
$action->register(new Cat1());
$action->register(new People());
$action->register(new Dog1());
$action->notify();
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
php利用header函數(shù)實(shí)現(xiàn)文件下載時(shí)直接提示保存
相信很多人在實(shí)現(xiàn)文件下載功能時(shí)會(huì)遇到這種情況,本意是點(diǎn)擊下載連接提示保存對(duì)話框,事情往往事與愿違,例如PDF這樣的文件會(huì)直接在瀏覽器中現(xiàn)實(shí)文件內(nèi)容,而不是提示保存。通過下面的方法可以解決這個(gè)問題。2009-11-11
php中類和對(duì)象:靜態(tài)屬性、靜態(tài)方法
這篇文章主要介紹了php中類和對(duì)象:靜態(tài)屬性、靜態(tài)方法,需要的朋友可以參考下2017-04-04
完美解決php 導(dǎo)出excle的.csv格式的數(shù)據(jù)時(shí)亂碼問題
下面小編就為大家?guī)硪黄昝澜鉀Qphp 導(dǎo)出excle的.csv格式的數(shù)據(jù)時(shí)亂碼問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
PHP轉(zhuǎn)Go之?dāng)?shù)組的正確使用詳解
這篇文章主要為大家對(duì)比一下PHP中的Array和Golang中的 Array&Slice&Map,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
php使用pdo連接mssql server數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了php使用pdo連接mssql server數(shù)據(jù)庫的方法,以實(shí)例形式分析了php使用pdo連接mssql server數(shù)據(jù)庫的技巧,非常簡單實(shí)用,需要的朋友可以參考下2014-12-12
用js進(jìn)行url編碼后用php反解以及用php實(shí)現(xiàn)js的escape功能函數(shù)總結(jié)
這次第一次用smarttemplate這個(gè)模板,比smarty小巧了很多,但也有些不方便的地方。2010-02-02
PHP中copy on write寫時(shí)復(fù)制機(jī)制介紹
這篇文章主要介紹了PHP中copy on write寫時(shí)復(fù)制機(jī)制介紹,需要的朋友可以參考下2014-05-05

