积木

No sub title

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

转载自: http://patmusing.blog.163.com/blog/static/135834960201002310460918/


假设我们需要开发一个坦克模拟系统用于模拟坦克在各种作战环境中的行为,其中坦克系统由引擎、车轮、控制器和火炮等各子系统构成。

12. C++实现Structural - Faccedil;ade模式 - 玄机逸士 - 玄机逸士博客

A方案的问题在于组件的客户和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和个子系统的演化,这种过多的耦合面临很多变化的挑战。Façade设计模式则简化外部客户程序和系统间的交互接口,将外部客户程序的演化和内部子系统的变化之间的依赖相互解耦。

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. (为子系统中的一组接口提供一个一致的界面,Façade设计模式定义了一个高层接口,这个接口使得这一子系统更加容易使用) - GoF

示例代码:

// Facade.h

#include <iostream>

#include <vector>

using namespace std;

class Engine // 发动机子系统

{

private:

bool engineStatus;

public:

Engine()

{

engineStatus = false;

}

public:

void turn_on() // 启动

{

engineStatus = true;

cout << "this is in method turn_on() ..." << endl;

}

void turn_off() // 关闭

{

engineStatus = false;

cout << "this is in method turn_off() ..." << endl;

}

bool get_engine_status() const // 获取发动机状态

{

return engineStatus;

}

};

class WheelPedrail // 履带子系统

{

public:

void rotate() // 转动

{

cout << "this in method rotate() ..." << endl;

}

};

class Controller // 控制子系统

{

public:

void forward() // 前进

{

cout << "this is in method forward() ..." << endl;

}

void backward() // 后退

{

cout << "this is in method backward() ..." << endl;

}

void turnright() // 右转

{

cout << "this is in method turnright() ..." << endl;

}

void turnleft() // 左转

{

cout << "this is in method turnleft() ..." << endl;

}

};

class Artillery // 火炮子系统

{

public:

void fire() // 开炮

{

cout << "this is in method fire() ..." << endl;

}

void shell_load() // 装填炮弹

{

cout << "this is in method shell_load() ..." << endl;

}

void aim_at() // 瞄准

{

cout << "this is in method aim_at() ..." << endl;

}

};

class TankFacade

{

private:

vector<Engine> engine;

vector<WheelPedrail> wheel_pedrail;

Controller controller;

Artillery artillery;

public:

TankFacade()

{

vector<Engine> eng(4); // 4个发动机

engine = eng;

for(int i = 0; i < 12; i++) // 12个轮子

{

WheelPedrail wp;

wheel_pedrail.push_back(wp);

}

}

void start()

{

if(!engine[0].get_engine_status()) engine[0].turn_on();

if(!engine[1].get_engine_status()) engine[1].turn_on();

if(!engine[2].get_engine_status()) engine[2].turn_on();

if(!engine[3].get_engine_status()) engine[3].turn_on();

}

void stop()

{

if(engine[0].get_engine_status()) engine[0].turn_off();

if(engine[1].get_engine_status()) engine[1].turn_off();

if(engine[2].get_engine_status()) engine[2].turn_off();

if(engine[3].get_engine_status()) engine[3].turn_off();

}

void run()

{

start();

for(int i = 0; i < 12; i++) // 12个轮子

{

wheel_pedrail[i].rotate();

}

controller.forward();

}

void fire()

{

start();

artillery.aim_at();

artillery.shell_load();

artillery.fire();

}

// ...

};

// 测试代码:Facade.cpp

#include "Facade.h"

int main(int argc, char **argv)

{

TankFacade *tank = new TankFacade;

tank->run();

tank->fire();

return 0;

}

程序运行结果:

this is in method turn_on() ...

this is in method turn_on() ...

this is in method turn_on() ...

this is in method turn_on() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this in method rotate() ...

this is in method forward() ...

this is in method aim_at() ...

this is in method shell_load() ...

this is in method fire() ...

Façade设计模式的几个要点:

- 从客户程序的角度来看,Façade设计模式不仅简化了整个组件系统的接口,同时对于组件内部与外部客户程序来说,从某种程度上也达到了一个“解耦”的效果 --- 内部子系统的任何变化不会影响到Façade接口的变化。

- Façade设计模式更注重从架构的层次去看整个系统,而不是从类的层次。Façade设计模式更多的时候是一种系统架构设计模式。

Façade设计模式、Adapter设计模式、Bridge设计模式与Decorator设计模式之间的区别:

- Façade设计模式注重简化接口;

- Adapter设计模式注重转换接口;

- Bridge设计模式注重分离接口(抽象)与其实现;

- Decorator设计模式注重在稳定接口的前提下为对象扩展功能。



posted on 2013-03-07 23:20 Jacc.Kim 阅读(249) 评论(0)  编辑 收藏 引用 所属分类: 设计模式

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理