定义
外观模式(Facade Pattern)是对复杂子系统的一种隐藏,该模式定义了一个高层接口,对客户端提供统一的访问界面,从而可以更方便的使用子系统。
代码实例
在公司中我们作为一线员工常常有各种申请,请假、涨薪、调休等,按照传统的纸质流程,要一个个部门的跑着签条子,现在引入了在线OA系统,各种申请通过OA系统可实现一键填写操作。我们通过外观模式组合了各部门之间的操作,客户端只需要填写申请事情就可以方便的操作负责审批的子系统完成工作了。
<?php
interface Company
{
public function examine($apply);
}
class Technical implements Company
{
public function examine($apply)
{
echo $apply . '技术部审批通过' . '<br>';
}
}
class Administration implements Company
{
public function examine($apply)
{
echo $apply . '人事部审批通过' . '<br>';
}
}
class GeneralManager implements Company
{
public function examine($apply)
{
echo $apply . '总经理审批通过' . '<br>';
}
}
class OneStopExamine
{
public $technical;
public $administration;
public $generalManager;
public function __construct()
{
$this->technical = new Technical();
$this->administration = new Administration();
$this->generalManager = new GeneralManager();
}
public function handle($apply)
{
$this->technical->examine($apply);
$this->administration->examine($apply);
$this->generalManager->examine($apply);
}
}
$oneStopExamine = new OneStopExamine();
$oneStopExamine->handle('phpworkerman 的涨薪申请');
总结
外观模式可以让客户端不必去关心复杂的子系统是如何工作的,解除了客户端和子系统间的耦合,复杂的操作全由外观类这个中间层来完成。但是外观模式会违反开闭原则,扩展不方便。
网友评论