两种语言实现设计模式(C++和Java)(九:桥接模式)

当设计的class具有多个维度的属性,用单继承的方式进行设计会造成设计出的子类很多,分类困难。

桥接模式将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。

桥接模式的的特点:

1.扩展能力强,实现和继承分离。

2.其实现细节对客户透明。

考虑装操作系统,有多种配置的计算机,同样也有多款操作系统。如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展,减少它们的耦合度。当然了,两者之间有标准的接口。这样设计,不论是对于计算机,还是操作系统都是非常有利的。

 

 

C++实现:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 //操作系统
 6 class OS
 7 {
 8 public:
 9     virtual void InstallOS_Imp() {}
10 };
11 class WindowsOS: public OS
12 {
13 public:
14     void InstallOS_Imp() { cout<<"安装Windows操作系统"<<endl; }
15 };
16 class LinuxOS: public OS
17 {
18 public:
19     void InstallOS_Imp() { cout<<"安装Linux操作系统"<<endl; }
20 };
21 class UnixOS: public OS
22 {
23 public:
24     void InstallOS_Imp() { cout<<"安装Unix操作系统"<<endl; }
25 };
26 
27 //计算机
28 class Computer
29 {
30 protected:
31     OS* os;
32 public:
33     Computer(OS* os){ this->os = os; }
34     virtual void InstallOS() {os->InstallOS_Imp();}
35 };
36 
37 class DellComputer: public Computer
38 {
39     using Computer::Computer;   //C++11中使用父类构造函数的新用法,替代DellComputer(Os *os): Computer(OS* os){}
40 public:
41     void InstallOS() {
42         cout << "Dell 电脑" << endl;
43         os->InstallOS_Imp();
44     }
45 };
46 
47 class AppleComputer: public Computer
48 {
49      using Computer::Computer;
50 public:
51     void InstallOS() {
52         cout << "苹果电脑" << endl;
53         os->InstallOS_Imp();
54     }
55 };
56 
57 class HPComputer: public Computer
58 {
59      using Computer::Computer;
60 public:
61     void InstallOS() {
62         cout << "惠普电脑" << endl;
63         os->InstallOS_Imp();
64     }
65 };
66 
67 int main()
68 {
69     OS *linuxOs = new LinuxOS();
70     OS *unixOs = new UnixOS();
71     Computer *dellComputer = new DellComputer(linuxOs);
72     Computer *appleComputer = new AppleComputer(unixOs);
73     dellComputer->InstallOS();
74     appleComputer->InstallOS();
75     return 0;
76 }

 

Java实现:

 1 public interface Os {
 2     public void installOsImpl();
 3 }
 4 
 5 public class LinuxOs implements Os {
 6 
 7     public void installOsImpl() {
 8         System.out.println("安装Linux操作系统");
 9     }
10 }
11 
12 public class WindowsOs implements Os {
13 
14     public void installOsImpl(){
15         System.out.println("安装Windows操作系统");
16     }
17 }
18 
19 public abstract class Computer {
20 
21     protected Os os;
22 
23     public Computer(Os os){
24         this.os = os;
25     }
26 
27     public void installOs(){
28         this.os.installOsImpl();
29     }
30 }
31 
32 public class DellComputer extends Computer {
33 
34     public DellComputer(Os os){
35         super(os);
36     }
37 
38     @Override
39     public void installOs() {
40         System.out.println("Dell 电脑");
41         super.installOs();
42     }
43 }
44 
45 public class LenovoComputer extends Computer{
46 
47     public LenovoComputer(Os os){
48         super(os);
49     }
50 
51     @Override
52     public void installOs() {
53         System.out.println("联想电脑");
54         super.installOs();
55     }
56 }
57 
58 public class Main {
59 
60     public static void main(String[] args){
61         LinuxOs linuxOs = new LinuxOs();
62         WindowsOs windowsOs = new WindowsOs();
63         Computer dellComputer = new DellComputer(linuxOs);
64         Computer lenovoComputer = new LenovoComputer(windowsOs);
65         dellComputer.installOs();
66         lenovoComputer.installOs();
67     }
68 
69 }

 

posted @ 2019-06-27 12:24  Asp1rant  阅读(214)  评论(0编辑  收藏  举报