Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170225
  • 博文数量: 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 23:08:06

桥接模式 不只改变你的实现,也改变你的抽象。在多维变化中应用,以简化结构,便于删增。




  1. /* AbstractMessage       ^
  2.  *                       |
  3.  * CommonMessage         +----o-----o-----o
  4.  *                       |    |     |     |
  5.  * UrgencyMessage        +----o-----o-----o
  6.  *                       |    |     |     |
  7.  * SpecialUrgencyMessage +----o-----o-----o
  8.  *                       |    |     |     |
  9.  *                       +----+-----+-----+-------------------->
  10.  *                            SMS   Email WeChat    MessageImplementor
  11.  */


  12. #include <iostream>

  13. using namespace std;

  14. struct MessageImplementor {
  15. public:
  16.     virtual void send(string message, string toUser) = 0;
  17. };

  18. struct AbstractMessage {
  19. public:
  20.     AbstractMessage(MessageImplementor *impl) {
  21.         this->impl = impl;
  22.     }
  23.     virtual void sendMessage(string message, string toUser) {
  24.         impl->send(message, toUser);
  25.     }
  26. private:
  27.     MessageImplementor *impl;
  28. };


  29. struct MessageSMS : public MessageImplementor {
  30. public:
  31.     virtual void send(string message, string toUser) {
  32.         cout << "Using SMS method, send message: " <<
  33.             message << " to " << toUser << endl;
  34.     }
  35. };

  36. struct MessageEmail : public MessageImplementor {
  37.     virtual void send(string message, string toUser) {
  38.         cout << "Using Email method, send message: " <<
  39.             message << " to " << toUser << endl;
  40.     }
  41. };

  42. struct MessageWeChat : public MessageImplementor {
  43.     virtual void send(string message, string toUser) {
  44.         cout << "Using Wechat method, send message: " <<
  45.             message << " to " << toUser << endl;
  46.     }
  47. };


  48. struct CommonMessage : public AbstractMessage {
  49.     CommonMessage(MessageImplementor *impl) :
  50.         AbstractMessage(impl) {}
  51.     virtual void sendMessage(string message, string toUser) {
  52.         AbstractMessage::sendMessage(message, toUser);
  53.     }
  54. };

  55. struct UrgencyMessage : public AbstractMessage {
  56.     UrgencyMessage(MessageImplementor *impl) :
  57.         AbstractMessage(impl) {}
  58.     virtual void sendMessage(string message, string toUser) {
  59.         message = "Urgency: " + message;
  60.         AbstractMessage::sendMessage(message, toUser);
  61.     }
  62.     void *watch(string messageId) {
  63.         return NULL;
  64.     }
  65. };

  66. struct SpecialUrgencyMessage : public AbstractMessage {
  67.     SpecialUrgencyMessage(MessageImplementor *impl) :
  68.         AbstractMessage(impl) {}
  69.     virtual void sendMessage(string message, string toUser) {
  70.         message = "Special Urgency: " + message;
  71.         AbstractMessage::sendMessage(message, toUser);
  72.     }
  73.     void *watch(string messageId) {
  74.         return NULL;
  75.     }
  76. };

  77. int main(int argc, char **argv)
  78. {
  79.     MessageImplementor *impl = new MessageSMS();

  80.     AbstractMessage *msg = new CommonMessage(impl);
  81.     msg->sendMessage("Hello", "Jack");
  82.     delete msg;

  83.     msg = new UrgencyMessage(impl);
  84.     msg->sendMessage("Hello", "Jack");
  85.     delete msg;

  86.     msg = new SpecialUrgencyMessage(impl);
  87.     msg->sendMessage("Hello", "Jack");
  88.     delete msg;

  89.     delete impl;

  90.     impl = new MessageEmail();

  91.     msg = new CommonMessage(impl);
  92.     msg->sendMessage("Hello", "Jack");
  93.     delete msg;

  94.     msg = new UrgencyMessage(impl);
  95.     msg->sendMessage("Hello", "Jack");
  96.     delete msg;

  97.     msg = new SpecialUrgencyMessage(impl);
  98.     msg->sendMessage("Hello", "Jack");
  99.     delete msg;
  100.     
  101.     delete impl;

  102.     impl = new MessageWeChat();

  103.     msg = new CommonMessage(impl);
  104.     msg->sendMessage("Hello", "Jack");
  105.     delete msg;

  106.     msg = new UrgencyMessage(impl);
  107.     msg->sendMessage("Hello", "Jack");
  108.     delete msg;

  109.     msg = new SpecialUrgencyMessage(impl);
  110.     msg->sendMessage("Hello", "Jack");
  111.     delete msg;

  112.     delete impl;
  113. }

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

上一篇:组合模式

下一篇:生成器模式

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