码迷,mamicode.com
首页 > Web开发 > 详细

php门面模式(facade pattern)

时间:2019-07-14 00:07:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:pat   for   most   quick   ems   模式   com   quic   rap   

书上不全的代码,我自己补全的。

<?php
/*
The facade pattern is used when we want to simplify the complexities of large
systems through a simpler interface. It does so by providing convenient methods
for most common tasks, through a single wrapper class used by a client.
*/

class Product {
    private $qty = null;
    
    public function __construct($qty) {
        $this->qty = $qty;
    }
    
    public function getQty() {
        echo ‘Product_getQty<br/>‘;
        return $this->qty;
    }
}

class QuickOrderFacade {
    private $product = null;
    private $orderQty = null;
    
    public function __construct($product, $orderQty) {
        $this->product = $product;
        $this->orderQty = $orderQty;
    }
    
    public function generateOrder() {
        if ($this->qtyCheck()) {
            $this->addToCart();
            $this->calculateShipping();
            $this->applyDiscount();
            $this->placeOrder();
        }
    }
    
    private function addToCart() {
        echo ‘QuickOrderFacade_addToCart<br/>‘;
    }
    
    private function qtyCheck() {
        if ($this->product->getQty() > $this->orderQty) {
            return true;
        } else {
            return false;
        }
    }
    
    private function calculateShipping() {
        echo ‘QuickOrderFacade_calculateShipping<br/>‘;
    }
    
    private function applyDiscount() {
        echo ‘QuickOrderFacade_applyDiscount<br/>‘;
    }
    
    private function placeOrder() {
        echo ‘QuickOrderFacade_placeOrder<br/>‘;
    }
}

$order = new QuickOrderFacade(new Product(8), 6);
$order->generateOrder();
?>

技术图片

php门面模式(facade pattern)

标签:pat   for   most   quick   ems   模式   com   quic   rap   

原文地址:https://www.cnblogs.com/aguncn/p/11182635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!