• C++ 外观模式的实现


     做笔记使用,如有写得不够好或者错误的地方还请指正!!

    1.外观模式 

    1. #include
    2. using namespace std;
    3. /*
    4. 外观模式(Facade Pattern)场景:
    5. 假设我们在淘宝购物,物品从网上到我们手里需要经过复杂的流程,而我们只需要通过淘宝点击购买下单即可,不需要关注商品在内部的流转流程
    6. 订单数据处理、客服订单确认、物流派送等
    7. */
    8. // 订单数据处理
    9. class Data {
    10. public:
    11. void run() {
    12. cout << "订单数据提交处理" << endl;
    13. }
    14. };
    15. // 客服订单确认
    16. class Check {
    17. public:
    18. void run() {
    19. cout << "客服订单确认" << endl;
    20. }
    21. };
    22. // 物流派送
    23. class Trans {
    24. public:
    25. void run() {
    26. cout << "物流派送" << endl;
    27. }
    28. };
    29. // 接口类,用户购买商品
    30. class Buy {
    31. private:
    32. Data* data;
    33. Check* check;
    34. Trans* trans;
    35. public:
    36. Buy() {
    37. data = new Data();
    38. check = new Check();
    39. trans = new Trans();
    40. }
    41. ~Buy() {
    42. delete data;
    43. delete check;
    44. delete trans;
    45. data = nullptr;
    46. check = nullptr;
    47. trans = nullptr;
    48. }
    49. void run() {
    50. data->run();
    51. check->run();
    52. trans->run();
    53. cout << "购物完成" << endl;
    54. }
    55. };
    56. int main() {
    57. Buy buy;
    58. buy.run();
    59. return 0;
    60. }

    2.外观模式转化为单例(懒汉式)单例模式C++实现icon-default.png?t=M666https://blog.csdn.net/weixin_44178960/article/details/126033100?spm=1001.2014.3001.5501

    1. #include
    2. using namespace std;
    3. // 订单数据处理
    4. class Data {
    5. public:
    6. void run() {
    7. cout << "订单数据提交处理" << endl;
    8. }
    9. };
    10. // 客服订单确认
    11. class Check {
    12. public:
    13. void run() {
    14. cout << "客服订单确认" << endl;
    15. }
    16. };
    17. // 物流派送
    18. class Trans {
    19. public:
    20. void run() {
    21. cout << "物流派送" << endl;
    22. }
    23. };
    24. // 接口类,用户购买商品
    25. class Buy {
    26. private:
    27. Data* data;
    28. Check* check;
    29. Trans* trans;
    30. // 私有化构造函数、析构函数、赋值运算符防止多个实例产生
    31. Buy() {
    32. data = new Data();
    33. check = new Check();
    34. trans = new Trans();
    35. }
    36. ~Buy() {
    37. delete data;
    38. delete check;
    39. delete trans;
    40. data = nullptr;
    41. check = nullptr;
    42. trans = nullptr;
    43. cout << "析构" << endl;
    44. }
    45. Buy& operator=(const Buy&) = delete;
    46. public:
    47. static Buy& getInstance() {
    48. static Buy buy;
    49. return buy;
    50. }
    51. void run() {
    52. data->run();
    53. check->run();
    54. trans->run();
    55. cout << "购物完成" << endl;
    56. }
    57. };
    58. int main() {
    59. Buy::getInstance().run();
    60. return 0;
    61. }

    溜了溜了~~~ 

  • 相关阅读:
    慢 SQL 的致胜法宝
    Ubuntu 18.04安装搜狗输入法后无法显示中文
    docker+网桥+redis主从+哨兵模式
    sentinel与nacos持久化
    连接池快速入门
    《Learning from Context or Names?An Empirical Study on Neural Relation Extraction》论文阅读笔记
    MySQL数据库基本操作
    【数学思维】运筹学-线性规划之标准形式与Hidden LP
    Gin中间件开发
    解决Jenkins执行git脚本时报错:No such device or address问题
  • 原文地址:https://blog.csdn.net/weixin_44178960/article/details/126129612