• 018 --- 第22章 桥接模式


    简述:

      桥接模式:将抽象部分与它的实现部分分离,是它们都可以独立的变化。

      桥接模式包括:抽象类、具体抽象类、实现类、具体实现类。

        抽象类:抽象要执行的操作。

        具体抽象类:包含实现类的指针,实现抽象类的虚函数。

        实现类:具体实现的抽象。

        具体实现类:实现类的具体实现。

    桥接模式:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 // 实现类
     5 class CImplementor
     6 {
     7 public:
     8     virtual void Operation() = 0;
     9 };
    10 
    11 // 具体实现类A
    12 class CConcreteImplementorA : public CImplementor
    13 {
    14 public:
    15     virtual void Operation()
    16     {
    17         cout << "具体实现A的方法执行" << endl;
    18     }
    19 };
    20 
    21 // 具体实现类B
    22 class CConcreteImplementorB : public CImplementor
    23 {
    24 public:
    25     virtual void Operation()
    26     {
    27         cout << "具体实现B的方法执行" << endl;
    28     }
    29 };
    30 
    31 // 抽象类
    32 class CAbstraction
    33 {
    34 protected:
    35     CImplementor* m_pImplementor;
    36     
    37 public:
    38     void SetImplementor(CImplementor* pImplementor)
    39     {
    40         m_pImplementor = pImplementor;
    41     }
    42 
    43     virtual void Operation()
    44     {
    45         m_pImplementor->Operation();
    46     }
    47 };
    48 
    49 // 具体抽象类
    50 class CRefinedAbstraction : public CAbstraction
    51 {
    52 public:
    53     virtual void Operation()
    54     {
    55         m_pImplementor->Operation();
    56     }
    57 };
    58 
    59 int main()
    60 {
    61     CAbstraction Abstraction;
    62     CConcreteImplementorA ImplementorA;
    63     Abstraction.SetImplementor(&ImplementorA);
    64     Abstraction.Operation();
    65 
    66     CConcreteImplementorB ImplementorB;
    67     Abstraction.SetImplementor(&ImplementorB);
    68     Abstraction.Operation();
    69 
    70     system("pause");
    71     return 0;
    72 }

    输出结果:

    例:手机品牌和手机软件桥接

    代码如下:

      1 #include <iostream>
      2 using namespace std;
      3 
      4 // 手机软件类(实现类)
      5 class CHandsetSoft
      6 {
      7 public:
      8     virtual void Run() = 0;
      9 };
     10 
     11 // 手机游戏类(具体实现类)
     12 class CHandsetGame : public CHandsetSoft
     13 {
     14 public:
     15     virtual void Run()
     16     {
     17         cout << "运行手机游戏" << endl;
     18     }
     19 };
     20 
     21 // 手机通讯录类(具体实现类)
     22 class CHandsetAddressList : public CHandsetSoft
     23 {
     24 public:
     25     virtual void Run()
     26     {
     27         cout << "运行手机通讯录" << endl;
     28     }
     29 };
     30 
     31 // 手机MP3播放类(具体实现类)
     32 class CHandsetMP3 : public CHandsetSoft
     33 {
     34 public:
     35     virtual void Run()
     36     {
     37         cout << "运行手机MP3播放" << endl;
     38     }
     39 };
     40 
     41 // 手机品牌类(抽象类)
     42 class CHandsetBrand
     43 {
     44 protected:
     45     CHandsetSoft* m_pSoft;
     46 
     47 public:
     48 
     49     // 设置手机软件
     50     void SetHandsetSoft(CHandsetSoft* pSoft)
     51     {
     52         m_pSoft = pSoft;
     53     }
     54 
     55     // 运行
     56     virtual void Run() = 0;
     57 };
     58 
     59 // 手机品牌N类(具体抽象类)
     60 class CHandsetBrandN : public CHandsetBrand
     61 {
     62 public:
     63     virtual void Run()
     64     {
     65         m_pSoft->Run();
     66     }
     67 };
     68 
     69 // 手机品牌M类(具体抽象类)
     70 class CHandsetBrandM : public CHandsetBrand
     71 {
     72 public:
     73     virtual void Run()
     74     {
     75         m_pSoft->Run();
     76     }
     77 };
     78 
     79 // 手机品牌S类(具体抽象类)
     80 class CHandsetBrandS : public CHandsetBrand
     81 {
     82 public:
     83     virtual void Run()
     84     {
     85         m_pSoft->Run();
     86     }
     87 };
     88 
     89 int main()
     90 {
     91     CHandsetGame Game;
     92     CHandsetAddressList AddressList;
     93     CHandsetMP3 MP3;
     94 
     95     cout << "手机N" << endl;
     96     CHandsetBrandN n;
     97     n.SetHandsetSoft(&Game);
     98     n.Run();
     99     n.SetHandsetSoft(&AddressList);
    100     n.Run();
    101     n.SetHandsetSoft(&MP3);
    102     n.Run();
    103     cout << "手机N" << endl;
    104     cout << endl;
    105 
    106     cout << "手机M" << endl;
    107     CHandsetBrandM m;
    108     m.SetHandsetSoft(&Game);
    109     m.Run();
    110     m.SetHandsetSoft(&AddressList);
    111     m.Run();
    112     m.SetHandsetSoft(&MP3);
    113     m.Run();
    114     cout << "手机M" << endl;
    115     cout << endl;
    116 
    117     cout << "手机S" << endl;
    118     CHandsetBrandS s;
    119     s.SetHandsetSoft(&Game);
    120     s.Run();
    121     s.SetHandsetSoft(&AddressList);
    122     s.Run();
    123     s.SetHandsetSoft(&MP3);
    124     s.Run();
    125     cout << "手机S" << endl;
    126     cout << endl;
    127 
    128     system("pause");
    129     return 0;
    130 }

    输出结果:

  • 相关阅读:
    2、requests练习
    1、接口基础
    9、异常和文件
    宝马5系图片分类下载自动创建文件夹并保存
    opencv操作视频python
    利用协程框架,无界面浏览器爬取上海高院开庭数据
    协程框架
    多线程抓取邮箱
    selenium操作下拉选和网页提示框
    selenium相关导入By、Keys、WebDriverWait、ActionChains,显示等待与隐式等待
  • 原文地址:https://www.cnblogs.com/SmallAndGreat/p/13633225.html
Copyright © 2020-2023  润新知