桥接模式找了很多资料,但还是比较迷糊,和装饰器模式比较似乎懂了一点,但还待补充,先上代码
1 // Bridge.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <string> 7 8 9 class os { 10 public: 11 virtual std::string getos() = 0; 12 }; 13 14 class ios :public os { 15 public: 16 virtual std::string getos() { 17 std::cout << "ios" << std::endl; 18 return "ios"; 19 } 20 }; 21 22 class harmonyos :public os { 23 public: 24 virtual std::string getos() { 25 std::cout << "harmonyos" << std::endl; 26 return "harmonyos"; 27 } 28 }; 29 30 31 class phone 32 { 33 public: 34 virtual void setos(os* s) = 0; 35 }; 36 37 class iphone :public phone 38 { 39 iphone() { 40 std::cout << "iphone" << std::endl; 41 } 42 public: 43 virtual void setos(os* pos) { 44 m_pOS = pos; 45 m_pOS->getos(); 46 } 47 private: 48 os* m_pOS; 49 }; 50 51 class huawei :public phone 52 { 53 public: 54 huawei() 55 { 56 std::cout << "huawei" << std::endl; 57 } 58 public: 59 virtual void setos(os* pos) { 60 m_pOS = pos; 61 m_pOS->getos(); 62 } 63 private: 64 os* m_pOS; 65 }; 66 67 int main() 68 { 69 os* pos = new ios(); 70 phone* pphone = new huawei(); 71 pphone->setos(pos); 72 system("pause"); 73 return 0; 74 }
原文:https://www.cnblogs.com/njit-sam/p/14924993.html