-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacade.php
56 lines (46 loc) · 1.06 KB
/
facade.php
1
<?php/** * 外观模式 * * 体现了封装的思想,将多个子系统的封装起来,提供一个简洁的接口供外部调用。在我们编程的过程中你不知不觉已经在使用这种设计模式 */class Goods { private $num=1; private $price=12; public function consumerStock($num) { $this->num = $num; echo "消耗库存:".$num.PHP_EOL; } public function getPrice() { return $this->price; } public function getScores() { return $this->price * $this->num; }}class User{ public function addScores(Goods $goods) { echo "用户获得:".$goods->getScores()."积分"; }}class Order { private $goods; private $user; public function __construct(Goods $goods, User $user) { $this->goods = $goods; $this->user = $user; } public function create($num) { $this->goods->consumerStock($num); $this->user->addScores($this->goods); }}$order = new Order(new Goods(), new User());$order->create(2);