018 --- 第22章 橋接模式
阿新 • • 發佈:2020-09-08
簡述:
橋接模式:將抽象部分與它的實現部分分離,是它們都可以獨立的變化。
橋接模式包括:抽象類、具體抽象類、實現類、具體實現類。
抽象類:抽象要執行的操作。
具體抽象類:包含實現類的指標,實現抽象類的虛擬函式。
實現類:具體實現的抽象。
具體實現類:實現類的具體實現。
橋接模式:
1 #include <iostream> 2 using namespace std; 3 4 // 實現類 5 class CImplementor 6 { 7 public: 8 virtual void Operation() = 0; 9 }; 10 11 // 具體實現類A 12 class CConcreteImplementorA : public CImplementor 13 { 14 public: 15 virtual void Operation() 16 { 17 cout << "具體實現A的方法執行" << endl; 18 } 19 }; 20 21 // 具體實現類B 22 class CConcreteImplementorB : public CImplementor 23 { 24 public: 25 virtual voidOperation() 26 { 27 cout << "具體實現B的方法執行" << endl; 28 } 29 }; 30 31 // 抽象類 32 class CAbstraction 33 { 34 protected: 35 CImplementor* m_pImplementor; 36 37 public: 38 void SetImplementor(CImplementor* pImplementor) 39 { 40 m_pImplementor = pImplementor;41 } 42 43 virtual void Operation() 44 { 45 m_pImplementor->Operation(); 46 } 47 }; 48 49 // 具體抽象類 50 class CRefinedAbstraction : public CAbstraction 51 { 52 public: 53 virtual void Operation() 54 { 55 m_pImplementor->Operation(); 56 } 57 }; 58 59 int main() 60 { 61 CAbstraction Abstraction; 62 CConcreteImplementorA ImplementorA; 63 Abstraction.SetImplementor(&ImplementorA); 64 Abstraction.Operation(); 65 66 CConcreteImplementorB ImplementorB; 67 Abstraction.SetImplementor(&ImplementorB); 68 Abstraction.Operation(); 69 70 system("pause"); 71 return 0; 72 }
輸出結果:
例:手機品牌和手機軟體橋接
程式碼如下:
1 #include <iostream> 2 using namespace std; 3 4 // 手機軟體類(實現類) 5 class CHandsetSoft 6 { 7 public: 8 virtual void Run() = 0; 9 }; 10 11 // 手機遊戲類(具體實現類) 12 class CHandsetGame : public CHandsetSoft 13 { 14 public: 15 virtual void Run() 16 { 17 cout << "執行手機遊戲" << endl; 18 } 19 }; 20 21 // 手機通訊錄類(具體實現類) 22 class CHandsetAddressList : public CHandsetSoft 23 { 24 public: 25 virtual void Run() 26 { 27 cout << "執行手機通訊錄" << endl; 28 } 29 }; 30 31 // 手機MP3播放類(具體實現類) 32 class CHandsetMP3 : public CHandsetSoft 33 { 34 public: 35 virtual void Run() 36 { 37 cout << "執行手機MP3播放" << endl; 38 } 39 }; 40 41 // 手機品牌類(抽象類) 42 class CHandsetBrand 43 { 44 protected: 45 CHandsetSoft* m_pSoft; 46 47 public: 48 49 // 設定手機軟體 50 void SetHandsetSoft(CHandsetSoft* pSoft) 51 { 52 m_pSoft = pSoft; 53 } 54 55 // 執行 56 virtual void Run() = 0; 57 }; 58 59 // 手機品牌N類(具體抽象類) 60 class CHandsetBrandN : public CHandsetBrand 61 { 62 public: 63 virtual void Run() 64 { 65 m_pSoft->Run(); 66 } 67 }; 68 69 // 手機品牌M類(具體抽象類) 70 class CHandsetBrandM : public CHandsetBrand 71 { 72 public: 73 virtual void Run() 74 { 75 m_pSoft->Run(); 76 } 77 }; 78 79 // 手機品牌S類(具體抽象類) 80 class CHandsetBrandS : public CHandsetBrand 81 { 82 public: 83 virtual void Run() 84 { 85 m_pSoft->Run(); 86 } 87 }; 88 89 int main() 90 { 91 CHandsetGame Game; 92 CHandsetAddressList AddressList; 93 CHandsetMP3 MP3; 94 95 cout << "手機N" << endl; 96 CHandsetBrandN n; 97 n.SetHandsetSoft(&Game); 98 n.Run(); 99 n.SetHandsetSoft(&AddressList); 100 n.Run(); 101 n.SetHandsetSoft(&MP3); 102 n.Run(); 103 cout << "手機N" << endl; 104 cout << endl; 105 106 cout << "手機M" << endl; 107 CHandsetBrandM m; 108 m.SetHandsetSoft(&Game); 109 m.Run(); 110 m.SetHandsetSoft(&AddressList); 111 m.Run(); 112 m.SetHandsetSoft(&MP3); 113 m.Run(); 114 cout << "手機M" << endl; 115 cout << endl; 116 117 cout << "手機S" << endl; 118 CHandsetBrandS s; 119 s.SetHandsetSoft(&Game); 120 s.Run(); 121 s.SetHandsetSoft(&AddressList); 122 s.Run(); 123 s.SetHandsetSoft(&MP3); 124 s.Run(); 125 cout << "手機S" << endl; 126 cout << endl; 127 128 system("pause"); 129 return 0; 130 }
輸出結果: