ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

软件设计⑨|桥接模式

2021-10-31 19:33:50  阅读:210  来源: 互联网

标签:__ run 桥接 void 模式 class 软件设计 public ROAD


 (1)C++

效果如下:

类图如下:

 

代码如下:

 1 #include <iostream>
 2 #ifndef __ROAD__H__
 3 #define __ROAD__H__
 4 using namespace std;
 5 
 6 class Road {
 7 public:
 8     virtual void run() = 0;
 9 };
10 
11 class UnpaveRoad : public Road {
12 public:
13     void run() override {
14         cout << "run on the speed way" << endl;
15     }
16 };
17 
18 class CementRoad : public Road {
19 public:
20     void run() override {
21         cout << "run on the street" << endl;
22     }
23 };
24 
25 class BusOnUnpaveRoad : public UnpaveRoad {
26 public:
27     void run() override {
28         cout << "Bus行驶在沥青路上" << endl;
29     }
30 };
31 
32 class CarOnUnpaveRoad : public UnpaveRoad {
33 public:
34     void run() override {
35         cout << "Car行驶在沥青路上" << endl;
36     }
37 };
38 
39 class BusOnCementRoad : public CementRoad {
40 public:
41     void run() override {
42         cout << "Bus行驶在水泥路上" << endl;
43     }
44 };
45 
46 class CarOnCementRoad : public CementRoad {
47 public:
48     void run() override {
49         cout << "Car行驶在水泥路上" << endl;
50     }
51 };
52 #endif
53 
54 int main() {
55     Road* busOnUnpaveRoad = new BusOnUnpaveRoad;
56     Road* carOnUnpaveRoad = new CarOnUnpaveRoad;
57     Road* busOnCementRoad = new BusOnCementRoad;
58     Road* carOnCementRoad = new CarOnCementRoad;
59 
60     busOnUnpaveRoad->run();
61     carOnUnpaveRoad->run();
62     busOnCementRoad->run();
63     carOnCementRoad->run();
64 
65     return 0;
66 }

参考链接:https://www.cnblogs.com/yuandonghua/p/11836249.html

标签:__,run,桥接,void,模式,class,软件设计,public,ROAD
来源: https://www.cnblogs.com/miao-com/p/15396467.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有