• 008 --- 第12章 外观模式


    简述:

      外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

      外观模式包括:外观类、子系统类。

        外观类:知道哪些子系统类负责处理请求,将客户的请求代理给适当的子系统对象。

        子系统类:实现子系统的功能,处理外观类对象指派的任务。注意子类中没有外观类的任何信息,即没有外观类对象属性。

    外观模式代码:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 // 外观模式
     5 // 子系统类
     6 class CSubSystemOne
     7 {
     8 public:
     9     void MethodOne()
    10     {
    11         cout << "子系统方法一" << endl;
    12     }
    13 };
    14 
    15 // 子系统类
    16 class CSubSystemTwo
    17 {
    18 public:
    19     void MethodTwo()
    20     {
    21         cout << "子系统方法二" << endl;
    22     }
    23 };
    24 
    25 // 子系统类
    26 class CSubSystemThree
    27 {
    28 public:
    29     void MethodThree()
    30     {
    31         cout << "子系统方法三" << endl;
    32     }
    33 };
    34 
    35 // 子系统类
    36 class CSubSystemFour
    37 {
    38 public:
    39     void MethodFour()
    40     {
    41         cout << "子系统方法四" << endl;
    42     }
    43 };
    44 
    45 // 外观类
    46 class CFacade
    47 {
    48 private:
    49     CSubSystemOne m_SubSystemOne;
    50     CSubSystemTwo m_SubSystemTwo;
    51     CSubSystemThree m_SubSystemThree;
    52     CSubSystemFour m_SubSystemFour;
    53 
    54 public:
    55     void MethodA()
    56     {
    57         cout << "方法组A() ----" << endl;
    58         m_SubSystemOne.MethodOne();
    59         m_SubSystemTwo.MethodTwo();
    60         m_SubSystemFour.MethodFour();
    61     }
    62 
    63     void MethodB()
    64     {
    65         cout << "方法组B() ----" << endl;
    66         m_SubSystemTwo.MethodTwo();
    67         m_SubSystemThree.MethodThree();
    68     }
    69 };
    70 
    71 int main()
    72 {
    73     CFacade Facade;
    74     Facade.MethodA();
    75     Facade.MethodB();
    76 
    77     system("pause");
    78     return 0;
    79 }

    输出结果:

    例:股票基金

    代码如下:

      1 #include <iostream>
      2 using namespace std;
      3 
      4 // 股票1(子系统类)
      5 class CStock1
      6 {
      7 public:
      8     void Sell()
      9     {
     10         cout << "股票1卖出" << endl;
     11     }
     12 
     13     void Buy()
     14     {
     15         cout << "股票1买入" << endl;
     16     }
     17 };
     18 
     19 // 股票2(子系统类)
     20 class CStock2
     21 {
     22 public:
     23     void Sell()
     24     {
     25         cout << "股票2卖出" << endl;
     26     }
     27 
     28     void Buy()
     29     {
     30         cout << "股票2买入" << endl;
     31     }
     32 };
     33 
     34 // 股票3(子系统类)
     35 class CStock3
     36 {
     37 public:
     38     void Sell()
     39     {
     40         cout << "股票3卖出" << endl;
     41     }
     42 
     43     void Buy()
     44     {
     45         cout << "股票3买入" << endl;
     46     }
     47 };
     48 
     49 // 国债1(子系统类)
     50 class CNationalDebt1
     51 {
     52 public:
     53     void Sell()
     54     {
     55         cout << "国债1卖出" << endl;
     56     }
     57 
     58     void Buy()
     59     {
     60         cout << "国债1买入" << endl;
     61     }
     62 };
     63 
     64 // 房地产1(子系统类)
     65 class CRealty1
     66 {
     67 public:
     68     void Sell()
     69     {
     70         cout << "房地产1卖出" << endl;
     71     }
     72 
     73     void Buy()
     74     {
     75         cout << "房地产1买入" << endl;
     76     }
     77 };
     78 
     79 // 基金类(外观类)
     80 class CFund
     81 {
     82 private:
     83     CStock1 m_Stock1;
     84     CStock2 m_Stock2;
     85     CStock3 m_Stock3;
     86     CNationalDebt1 m_NationalDebt1;
     87     CRealty1 m_Realty1;
     88 
     89 public:
     90     void BuyFund()
     91     {
     92         m_Stock1.Buy();
     93         m_Stock2.Buy();
     94         m_Stock3.Buy();
     95         m_NationalDebt1.Buy();
     96         m_Realty1.Buy();
     97     }
     98 
     99     void SellFund()
    100     {
    101         m_Stock1.Sell();
    102         m_Stock2.Sell();
    103         m_Stock3.Sell();
    104         m_NationalDebt1.Sell();
    105         m_Realty1.Sell();
    106     }
    107 };
    108 
    109 int main()
    110 {
    111     CFund Fund;
    112     Fund.BuyFund();
    113     Fund.SellFund();
    114 
    115     system("pause");
    116     return 0;
    117 }

    输出结果:

  • 相关阅读:
    第一个 Python 程序
    Qt之字体文件(TTF)
    Memcached
    Qt之QtSoap(访问WebService)
    Crypto++编译使用
    Memcached
    Windows下编译OpenSSL
    基于Core Text实现的TXT电子书阅读器
    java代码获取jdbc链接properties
    ext树表+ZeroClipboard复制链接功能
  • 原文地址:https://www.cnblogs.com/SmallAndGreat/p/13475940.html
Copyright © 2020-2023  润新知