Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170257
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:52:15

外观模式 提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。




  1. #include <iostream>
  2. #include <string>


  3. using namespace std;


  4. struct DvdPlayer {
  5. public:
  6.     on() {
  7.         cout << "DvdPlayer on" << endl;
  8.     }
  9.     off() {
  10.         cout << "DvdPlayer off" << endl;
  11.     }
  12.     play(string movie) {
  13.         cout << "DvdPlayer play: " << movie << endl;
  14.     }
  15.     stop() {
  16.         cout << "DvdPlayer stop" << endl;
  17.     }
  18. };

  19. struct Amplifier {
  20. public:
  21.     on() {
  22.         cout << "Amplifier on" << endl;
  23.     }
  24.     off() {
  25.         cout << "Amplifier off" << endl;
  26.     }
  27.     setDvd(DvdPlayer *mdvdPlayer) {
  28.         dvdPlayer = mdvdPlayer;
  29.         cout << "Amplifier set DvdPlayer" << endl;
  30.     }
  31.     setVolume(int value) {
  32.         cout << "Amplifier set volume to " << value << endl;
  33.     }
  34. private:
  35.     DvdPlayer *dvdPlayer;
  36. };

  37. struct Projector {
  38. public:
  39.     on() {
  40.         cout << "Projector on" << endl;
  41.     }
  42.     off() {
  43.         cout << "Projector off" << endl;
  44.     }
  45. };

  46. struct HomeThreaterFacade {
  47. public:
  48.     HomeThreaterFacade(Amplifier *mamp,
  49.         DvdPlayer *mdvd,
  50.         Projector *mprojector) {
  51.         amp = mamp;
  52.         dvd = mdvd;
  53.         projector = mprojector;
  54.     }

  55.     watchMovie(string movie) {
  56.         cout << "Get ready to watch a movie..." << endl;
  57.         projector->on();
  58.         projector->off();
  59.         amp->on();
  60.         amp->setDvd(dvd);
  61.         amp->setVolume(5);
  62.         dvd->on();
  63.         dvd->play(movie);
  64.     }

  65.     endMoive() {
  66.         cout << "Shutting movie theater down..." << endl;
  67.         projector->off();
  68.         amp->off();
  69.         dvd->stop();
  70.         dvd->off();
  71.     }

  72. private:
  73.     Amplifier *amp;
  74.     DvdPlayer *dvd;
  75.     Projector *projector;
  76. };

  77. int main(int argc, char **argv)
  78. {
  79.     Projector *projector = new Projector();
  80.     Amplifier *amp = new Amplifier();
  81.     DvdPlayer *dvd = new DvdPlayer();
  82.     HomeThreaterFacade *threater =
  83.         new HomeThreaterFacade(
  84.             amp, dvd, projector);

  85.     threater->watchMovie("freedom");
  86.     cout << "-----------------------" << endl;
  87.     threater->endMoive();

  88.     delete threater;
  89.     delete dvd;
  90.     delete amp;
  91.     delete projector;

  92.     return 0;
  93. }

阅读(665) | 评论(0) | 转发(0) |
0

上一篇:适配器模式

下一篇:模板方法模式

给主人留下些什么吧!~~