详解PHP中的外观模式facade pattern(2)

class SendMailFacade { private $sendMail; public function __construct(SendMailInterface $sendMail) { $this->sendMail = $sendMail; } public function setTo($to) { $this->sendMail->setSendToEmailAddress($to); return $this; } public function setSubject($subject) { $this->sendMail->setSubjectName($subject); return $this; } public function setBody($body) { $this->sendMail->setTheEmailContents($body); return $this; } public function setHeaders($headers) { $this->sendMail->setTheHeaders($headers); return $this; } public function send() { $this->sendMail->sendTheEmailNow(); } }

然后原来不加优化的终端调用可能是这样的:

$sendMail = new SendMail(); $sendMail->setSendToEmailAddress($to); $sendMail->setSubjectName($subject); $sendMail->setTheEmailContents($body); $sendMail->setTheHeaders($headers); $sendMail->sendTheEmailNow();

现在有了外观类,就可以这样了:

$sendMail = new SendMail(); $sendMailFacade = new sendMailFacade($sendMail); $sendMailFacade->setTo($to)->setSubject($subject)->setBody($body)->setHeaders($headers)->send();

示例三:完成一个商品交易的复杂流程

假设呢,一个商品交易环节需要有这么几步:

$productID = $_GET['productId']; $qtyCheck = new productQty(); // 检查库存 if($qtyCheck->checkQty($productID) > 0) { // 添加商品到购物车 $addToCart = new addToCart($productID); // 计算运费 $shipping = new shippingCharge(); $shipping->updateCharge(); // 计算打折 $discount = new discount(); $discount->applyDiscount(); $order = new order(); $order->generateOrder(); }

可以看到,一个流程呢包含了很多步骤,涉及到了很多Object,一旦类似环节要用在多个地方,可能就会导致问题,所以可以先创建一个外观类:

class productOrderFacade { public $productID = ''; public function __construct($pID) { $this->productID = $pID; } public function generateOrder() { if($this->qtyCheck()) { $this->addToCart(); $this->calulateShipping(); $this->applyDiscount(); $this->placeOrder(); } } private function addToCart () { /* .. add product to cart .. */ } private function qtyCheck() { $qty = 'get product quantity from database'; if($qty > 0) { return true; } else { return true; } } private function calulateShipping() { $shipping = new shippingCharge(); $shipping->calculateCharge(); } private function applyDiscount() { $discount = new discount(); $discount->applyDiscount(); } private function placeOrder() { $order = new order(); $order->generateOrder(); } }

这样呢,我们的终端调用就可以两行解决:

$order = new productOrderFacade($productID); $order->generateOrder();

示例四:往多个社交媒体同步消息的流程

// 发Twitter消息 class CodeTwit { function tweet($status, $url) { var_dump('Tweeted:'.$status.' from:'.$url); } } // 分享到Google plus上 class Googlize { function share($url) { var_dump('Shared on Google plus:'.$url); } } //分享到Reddit上 class Reddiator { function reddit($url, $title) { var_dump('Reddit! url:'.$url.' title:'.$title); } }

如果每次我们写了一篇文章,想着转发到其他平台,都得分别去调用相应方法,这工作量就太大了,后期平台数量往往只增不减呢。这个时候借助于facade class:

class shareFacade { protected $twitter; protected $google; protected $reddit; function __construct($twitterObj,$gooleObj,$redditObj) { $this->twitter = $twitterObj; $this->google = $gooleObj; $this->reddit = $redditObj; } function share($url,$title,$status) { $this->twitter->tweet($status, $url); $this->google->share($url); $this->reddit->reddit($url, $title); } }

这样终端调用就可以:

$shareObj = new shareFacade($twitterObj,$gooleObj,$redditObj); $shareObj->share('//myBlog.com/post-awsome','My greatest post','Read my greatest post ever.');

facade pattern的优劣势

优势

能够使你的终端调用与背后的子系统逻辑解耦,这往往发生在你的controller里,就意味着你的controller可以有更少的依赖,controller关注的更少了,从而责任和逻辑也更明确了,同时也意味着你子系统里的逻辑更改,并不会影响到你的controller里终端调用。

劣势

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/101db486279ad45021a6dc86f04851cc.html