Php设计模式之【外观模式Facade Pattern】
来源:互联网 发布:c语言预处理程序停止 编辑:程序博客网 时间:2024/05/30 04:31
【案例】
黑枣设计公司开发一套流程图开发工具。使用这套开发工具在画一个箭头时,用户先画一个三角,再画一个坚杠就可以了。不过用户反馈画箭头太麻烦,希望能精简操作。
<代码模拟>
class base{ public function draw($char = '*', $num = 1) { echo str_repeat($char, $num); }}class Triangle extends base{ public function __construct() { $this->draw(' ', 10); $this->draw('*', 1); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 8); $this->draw('*', 5); $this->draw("\n", 1); $this->draw(' ', 7); $this->draw('*', 7); $this->draw("\n", 1); $this->draw(' ', 6); $this->draw('*', 9); $this->draw("\n", 1); }}class CaineStick extends base{ public function __construct() { $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); }}class testDriver{ public function run() { $step1 = new Triangle(); $step2 = new CaineStick(); }}$test = new testDriver();$test->run();
【分析OOA】
在应用进程中的一个步骤抱很许多复杂的逻辑步骤和方法调用时,创建一个外观模式的对象,使用外观模式隐藏了自调用对象的复杂性问题。我们可以新建一个箭头类,箭头类将代码中的step1\step2封装起来。对于用户来说,不用先画三角再画竖杠,直接调用箭头类就可以了。
【设计OOD】
<UML>
<说明>
外观类FacedeArrow:这个外观类为用户提供一个画箭头的简单接口,该类依赖于Triangle三角及CaineStick竖杠类
【编程 OOP】
<代码>
class base{ public function draw($char = '*', $num = 1) { echo str_repeat($char, $num); }}class Triangle extends base{ public function __construct() { $this->draw(' ', 10); $this->draw('*', 1); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 8); $this->draw('*', 5); $this->draw("\n", 1); $this->draw(' ', 7); $this->draw('*', 7); $this->draw("\n", 1); $this->draw(' ', 6); $this->draw('*', 9); $this->draw("\n", 1); }}class CaineStick extends base{ public function __construct() { $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); $this->draw(' ', 9); $this->draw('*', 3); $this->draw("\n", 1); }}//箭头类class FacadeArrow extends base{ public function __construct(){ $step1 = new Triangle(); $step2 = new CaineStick(); }}
【测试用例Test Case】
<代码>
class testDriver{ public function run() { $step1 = new FacadeArrow(); }}$test = new testDriver();$test->run();
【输出】
小结:
【模式实现要点】
外观模式是软件工程中常用的一种软件设计模式。它为子系统中的一组接口提供一个统一的高层接口。使用子系统更容易使用。
【优点】
外观模式作为结构型模式中的一个简单又实用的模式,外观模式通过封装细节来提供大粒度的调用,直接的好处就是,封装细节,提供了应用写程序的可维护性和易用性。
【适用场景】
外观模式一般应用在系统架构的服务层中,当我们是多个不同类型的客户端应用程序时,比如一个系统既可以在通过Web的形式访问,也可以通过客户端应用程序的形式时,可能通过外观模式来提供远程服务,让应用程序进行远程调用,这样通过外观形式提供服务,那么不管是什么样的客户端都访问一致的外观服务,那么以后就算是我们的应用服务发生变化,那么我们不需要修改没一个客户端应用的调用,只需要修改相应的外观应用即可。
********************************************
* 作者:叶文涛
* 标题:Php设计模式之【外观模式[Facade Pattern】
* 参考:
*《设计模式:可复用面向对象软件基础 》(美)Erich Gamma 等著
*《Head First设计模式》Eric Freeman等著
* http://www.cnblogs.com/hegezhou_hot/archive/2010/12/06/1897398.html
******************转载请注明网址来源 ***************
- Php设计模式之【外观模式Facade Pattern】
- 设计模式之外观模式(Facade Pattern)
- 设计模式之 -- Facade pattern (外观模式)
- 设计模式之四:外观模式(Facade Pattern)
- 设计模式之 - 外观模式 (Facade design pattern)
- C#设计模式之外观模式(Facade Pattern)
- 设计模式(结构型)之外观模式(Facade Pattern)
- 设计模式之:外观模式(Facade Pattern)
- Java设计模式之--外观模式(facade pattern)
- 《设计模式解析》读书笔记之Facade Pattern(外观模式)
- 设计模式总结之Facade Pattern(外观模式)
- 设计模式 - Facade Pattern(外观模式)
- 设计模式[7] Facade Pattern 外观模式
- 设计模式 - Facade Pattern(外观模式)
- 设计模式-外观模式(Facade Pattern)
- 设计模式-外观模式(Facade Pattern)
- 设计模式 - 外观模式(facade pattern) 详解
- 设计模式-外观模式(facade pattern)
- Android HAL层实现与调用
- 单选按钮分组
- Zend Framework Module之多模块配置
- android 解析json数据格式
- C# 的 ArrayList
- Php设计模式之【外观模式Facade Pattern】
- Android之Cursor的用法
- Eclipse中将项目导出jar包,以及转化成exe文件
- android json解析
- MLO
- c语言access函数
- PHP文件缓存技术之页面静态化
- 我们是如此给力csdn,希望csdn继密码泄露事件以后,继续给力下去
- 读取Rockey4