php设计模式

模板方法模式(钩子的使用):

<?php
abstract class IHook {
    protected $purchased;
    protected $hookSpecial;
    protected $shippingHook;
    protected $fullCost;
    public function templateMethod($total, $special) {
        $this->purchased = $total;
        $this->hookSpecial = $special;
        $this->addTax();
        $this->addShippingHook();
        $this->displayCost();
    }
    protected abstract function addTax();
    protected abstract function addShippingHook();
    protected abstract function displayCost();
}
class ZambeziCalc extends IHook {
    protected function addTax() {
        $this->fullCost = $this->purchased + ($this->purchased * .07);
    }
    protected function addShippingHook() {
        if(! $this->hookSpecial) {
            $this->fullCost += 12.95;
        }
    }
    protected function displayCost() {
        echo "Your full cost id $this->fullCost";
    }
}
class Client {
    private $buyTotal;
    private $gpsNow;
    private $mapNow;
    private $boatNow;
    private $special;
    private $zamCalc;
    function __construct()
    {
        $this->setHTML();
        $this->setCost();
        $this->zamCalc = new ZambeziCalc();
        $this->zamCalc->templateMethod($this->buyTotal, $this->special);
    }
    private function setHTML() {
        $this->gpsNow = $_POST['gps'];
        $this->mapNOW = $_POST['map'];
        $this->boatNOW = $_POST['boat'];
    }
    private function setCost() {
        $this->buyTotal = $this->gpsNow;
        foreach($this->mapNow as $value) {
            $this->buyTotal += $value;
        }
        $this->special = ($this->buyTotal >= 200);
        $this->buyTotal += $this->boatNow;
    }
}
$worker = new Client();

状态设计模式-最简单的例子(开灯关灯):

<?php
class context {
    private $offState;
    private $onState;
    private $currentState;
    public function __construct()
    {
        $this->offState = new OffState($this);
        $this->onState = new OnState($this);
        //开始状态为off
        $this->currentState = $this->offState;
    }
    //调用状态方法触发器
    public function turnOnLight() {
        $this->currentState->turnLightOn();
    }
    public function turnOffLight() {
        $this->currentState->turnLightOff();
    }
    //设置当前状态
    public function setState(IState $state) {
        $this->currentState = $state;
    }
    //获得状态
    public function getOnstate() {
        return $this->onState;
    }
    public function getOffState() {
        return $this->offState;
    }
}
interface IState {
    public function turnLightOn();
    public function turnLightOff();
}
class OnState implements IState {
    private $context;
    public function __construct(Context $contextNow)
    {
        $this->context = $contextNow;
    }
    public function turnLightOn()
    {
        // TODO: Implement turnLightOn() method.
        echo "Light is already on-> take no action<br/>";
    }
    public function turnLightOff()
    {
        // TODO: Implement turnLightOff() method.
        echo "Lights off!<br/>";
        $this->context->setState($this->context->getOffState());
    }
}
class OffState implements IState {
    private $context;
    public function __construct(Context $contextNow)
    {
        $this->context = $contextNow;
    }
    public function turnLightOn()
    {
        // TODO: Implement turnLightOn() method.
        echo "Light on! Now I can see!<br/>";
        $this->context->setState($this->context->getOnState());
    }
    public function turnLightOff()
    {
        // TODO: Implement turnLightOff() method.
        echo "Lights is already off-> take no action<br/>";
    }
}
class Client {
    private $context;
    public function __construct()
    {
        $this->context = new Context();
        $this->context->turnOnLight();
        $this->context->turnOnLight();
        $this->context->turnOffLight();
        $this->context->turnOffLight();
    }
}
$worker = new Client();

Mysql和PHP设计模式

代理模式(通用类负责连接,代理模式保证安全)

  • 远程代理
  • 虚拟代理
  • 保护代理
  • 智能引用

观察者模式

<?php
class ConcreteSubject implements SplSubject {
    private $observers;
    private $data;
    public function setObservers() {
        $this->observers = new SplObjectStorage();
    }
    public function attach(SplObserver $observer)
    {
        // TODO: Implement attach() method.
        $this->observers->attach($observer);
    }
    public function detach(SplObserver $observer)
    {
        // TODO: Implement detach() method.
        $this->observers->detach($observer);
    }
    public function notify()
    {
        // TODO: Implement notify() method.
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }
    public function setData($dataNow) {
        $this->data = $dataNow;
    }
    public function getData() {
        return $this->data;
    }
}
class ConcreteObserver implements SplObserver {
    public function update(SplSubject $subject)
    {
        // TODO: Implement update() method.
        echo $subject->getData() . "<br/>";
    }
}
class Client {
    public function __construct()
    {
        echo "<p>Create three new concrete observers, a new concrete subject:</p>";
        $ob1 = new ConcreteObserver();
        $ob2 = new ConcreteObserver();
        $ob3 = new ConcreteObserver();
        $subject = new ConcreteSubject();
        $subject->setObservers();
        $subject->setData("Here's your data!");
        $subject->attach($ob1);
        $subject->attach($ob2);
        $subject->attach($ob3);
        $subject->notify();
        echo "<p>Detach observer ob3. Now only ob1 and ob2 are notified:</p>";
        $subject->detach($ob3);
        $subject->notify();
        echo "<p>Reset data and reattach ob3 and detach ob2--only ob 1 and 3 are notified:</p>";
        $subject->setData("More data that only ob1 and ob3 need.");
        $subject->attach($ob3);
        $subject->attach($ob2);
        $subject->notify();
    }
}
$worker = new Client();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值