php门面模式(facade pattern)

Posted aguncn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php门面模式(facade pattern)相关的知识,希望对你有一定的参考价值。

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

<?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)的主要内容,如果未能解决你的问题,请参考以下文章

外观/门面模式(Facade Pattern)

门面模式-Facade Pattern(Java实现)

编程思想设计模式结构模式Structural门面模式/外观模式Facade

PHP设计模式 - 门面模式

门面模式 facade

软件设计模式——门面模式(Facade)