设计模式——桥接模式

一、任务

用桥接模式实现在路上开车这个问题,其中,车可以是carbus,路可以是水泥路或沥青路

二、类图

三、代码

1、Java

1 package tutorial9;
2 
3 public interface AbstractRoad {
4 
5     public String roadRun();
6 }
AbstractRoad.java
 1 package tutorial9;
 2 
 3 public class AsphaltRoad implements AbstractRoad {
 4 
 5     @Override
 6     public String roadRun() {
 7         // TODO 自动生成的方法存根
 8         
 9         return "在沥青路上行驶";
10     }
11 
12 }
AsphaltRoad.java
 1 package tutorial9;
 2 
 3 public class Cement implements AbstractRoad {
 4 
 5     @Override
 6     public String roadRun() {
 7         // TODO 自动生成的方法存根
 8         
 9         return "在水泥路上行驶";
10     }
11 
12 }
Cement
 1 package tutorial9;
 2 
 3 public abstract class Vehicle {
 4     protected AbstractRoad road;
 5     
 6     public Vehicle(AbstractRoad road) {
 7         this.road=road;
 8     }
 9     
10     public abstract void run();
11 
12 }
Vehicle
 1 package tutorial9;
 2 
 3 import java.util.Random;
 4 
 5 public abstract class RefinedVehicle extends Vehicle {
 6 
 7     public RefinedVehicle(AbstractRoad road) {
 8         super(road);
 9         // TODO 自动生成的构造函数存根
10     }
11     
12     public void chooseWay(String str1) {
13         String str=str1+road.roadRun();
14         System.out.println(str);
15     }
16 
17 }
RefinedVehicle
 1 package tutorial9;
 2 
 3 public class Car extends RefinedVehicle{
 4 
 5     public Car(AbstractRoad road) {
 6         super(road);
 7         // TODO 自动生成的构造函数存根
 8     }
 9 
10     @Override
11     public void run() {
12         // TODO 自动生成的方法存根
13         System.out.println("小汽车");
14     }
15 
16 }
Car
 1 package tutorial9;
 2 
 3 public class Bus extends RefinedVehicle {
 4 
 5     public Bus(AbstractRoad road) {
 6         super(road);
 7         // TODO 自动生成的构造函数存根
 8     }
 9 
10     @Override
11     public void run() {
12         // TODO 自动生成的方法存根
13         System.out.println("公交车");
14     }
15 
16 }
Bus
 1 package tutorial9;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Client {
 6 
 7     public static void main(String[] args) {
 8         System.out.println("*************************");
 9         System.out.println("请选择车辆:");
10         System.out.println("1、小汽车");
11         System.out.println("2、公交车");
12         Scanner in=new Scanner(System.in);
13         int choice=in.nextInt();
14 
15         System.out.println("*************************");
16         System.out.println("请选择道路:");
17         System.out.println("1、水泥路");
18         System.out.println("2、沥青路");
19         int choice2=in.nextInt();
20         
21         
22         if(choice==1) {
23             switch(choice2) {
24             
25                 case 1:{
26                     RefinedVehicle vehicle=new Car(new Cement());
27                     vehicle.chooseWay("小汽车");
28                 }break;
29                 case 2:{
30                     RefinedVehicle vehicle2=new Car(new AsphaltRoad());
31                     vehicle2.chooseWay("小汽车");
32                 }break;
33             
34             }
35         }else if(choice==2) {
36             switch(choice2) {
37             case 1:{
38                 RefinedVehicle vehicle=new Bus(new Cement());
39                 vehicle.chooseWay("公交车");
40             }break;
41             case 2:{
42                 RefinedVehicle vehicle2=new Bus(new AsphaltRoad());
43                 vehicle2.chooseWay("公交车");
44             }break;
45         
46         }
47         }
48         
49         
50     } 
51 }
Client

2、C++

  1 #include<iostream>
  2 using namespace std;
  3 
  4 //抽象路
  5 class AbstractRoad {
  6 
  7 public:
  8     virtual string roadRun()=0;
  9 };
 10 
 11 class Vehicle {
 12 
 13 
 14 public:
 15     
 16     Vehicle() {
 17 
 18     }
 19     virtual void run() {};
 20 
 21 };
 22 
 23 
 24 //沥青路
 25 class AsphaltRoad :public AbstractRoad {
 26 
 27 public:
 28     string roadRun() {
 29 
 30         return "在沥青路上行驶";
 31     }
 32 
 33 };
 34 
 35 //水泥路
 36 class Cement :public AbstractRoad {
 37 
 38     
 39 public:
 40     string roadRun() {
 41 
 42         return "在水泥路上行驶";
 43     }
 44 
 45 };
 46 
 47 
 48 class RefinedVehicle : public  Vehicle {
 49 
 50 public:
 51     RefinedVehicle() {
 52 
 53     }
 54     
 55 
 56     
 57 
 58 };
 59 //
 60 class Car :public RefinedVehicle {
 61 private:
 62     AbstractRoad *road;
 63 public:
 64     Car(AbstractRoad *road1) {
 65         road = road1;
 66     }
 67     void run() {
 68         // TODO 自动生成的方法存根
 69         cout<<"小汽车"+road->roadRun()<<endl;
 70     }
 71 
 72 };
 73 
 74 class Bus :public RefinedVehicle {
 75 private:
 76     AbstractRoad* road;
 77 
 78 
 79 public:
 80     Bus(AbstractRoad* road1) {
 81         road = road1;
 82     }
 83     void run() {
 84 
 85         cout << "公交车" + road->roadRun() << endl;
 86     }
 87 
 88 };
 89 
 90 int main() {
 91 
 92     cout<<"*************************"<<endl;
 93     cout << "请选择车辆:"<<endl;
 94     cout << "1、小汽车"<<endl;
 95     cout << "2、公交车"<<endl;
 96     int choice = 0;
 97     cin >> choice;
 98     cout << "*************************" << endl;
 99     cout << "请选择道路:" << endl;
100     cout << "1、水泥路"<<endl;
101     cout << "2、沥青路" << endl;
102     int choice2 =0;
103     cin >> choice2;
104 
105     if (choice == 1) {
106         switch (choice2) {
107 
108         case 1: {
109             RefinedVehicle *vehicle = new Car(new Cement());
110             vehicle->run();
111         }break;
112         case 2: {
113             RefinedVehicle *vehicle2 = new Car(new AsphaltRoad());
114             vehicle2->run();
115         }break;
116 
117         }
118     }
119     else if (choice == 2) {
120         switch (choice2) {
121         case 1: {
122             
123             RefinedVehicle *vehicle = new Bus(new Cement());
124             vehicle->run();
125         }break;
126         case 2: {
127             RefinedVehicle *vehicle2 = new Bus(new AsphaltRoad());
128             vehicle2->run();
129         }break;
130 
131         }
132     }
133     return 0;
134 }
Client

四、结果截图

1、Java截图

2、C++截图

原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/13423422.html