1定義 將抽象和實現解耦,使得兩者可以獨立變化 2類圖 3實現 4應用 ①優點 抽象和實現分離 完全為瞭解決集成的缺點而提出的設計模式////抽象與實現的關係本來是縱向的,橋梁模式將他們改為橫向關係 優秀的擴充能力 實現細節對客戶透明 5使用場景 不希望或者不適合通過繼承的場景 介面或者抽象類不穩定 ...
1定義
將抽象和實現解耦,使得兩者可以獨立變化
2類圖
3實現
#pragma once #include<iostream> using namespace std; class Implementor { protected: Implementor(){} public: virtual ~Implementor() = 0{} virtual void doSomething() = 0; virtual void doAnything() = 0; }; class ConcreteImplementor1 :public Implementor { public: void doSomething() { cout << "1doSomething();" << endl; } void doAnything() { cout << "1doAnything();" << endl; } }; class ConcreteImplementor2 :public Implementor { public: void doSomething() { cout << "2doSomething();" << endl; } void doAnything() { cout << "2doAnything();" << endl; } }; class Abstraction { private: Implementor *_imp; public: Abstraction(Implementor * imp) :_imp(imp) {} virtual void request() { _imp->doSomething(); } Implementor* getImp() { return _imp; } }; class RefinedAbstraction :public Abstraction { public: RefinedAbstraction(Implementor* imp) :Abstraction(imp) {} void request() { Abstraction::request(); Abstraction::getImp()->doAnything(); } }; class Client { public: void operator()() { Implementor* imp = new ConcreteImplementor1(); Abstraction* abs = new RefinedAbstraction(imp); abs->request(); } };
4應用
①優點
抽象和實現分離
完全為瞭解決集成的缺點而提出的設計模式////抽象與實現的關係本來是縱向的,橋梁模式將他們改為橫向關係
優秀的擴充能力
實現細節對客戶透明
5使用場景
不希望或者不適合通過繼承的場景
介面或者抽象類不穩定
重要性要求較高的場景
6註意事項
使用橋梁模式重點在於如何拆分抽象和實現,並不是以設計繼承就考慮使他,橋梁的意圖是對變化進行封裝