Bridging Mode: separates object from specific behaviors and features so that they can change independently. Object is only an abstract concept. For example, "circle" and "Triangle" are under the abstract "shape", while "circle" and "Triangle draw" are under the "Drawing" Class of implementation behavior, then, "Drawing" is called by "shape ". "Shape" becomes an inheritance system, and "Drawing" becomes another inheritance system. The relationship between abstraction and implementation is an aggregation relationship. The UML diagram is as follows:
- Define action: defines an abstract interface that contains the implementor interface that implements specific behaviors and features.
- Refined transaction action: a subclass of The abstracted action interface. It is still an abstract object name.
- Implementor: An application interface that defines specific behaviors and features
- Concreteimplementor: implements the implementor interface.
The following is the Bridge Mode framework described in C ++:
# Include <iostream >#include <string> using namespace STD; // implement class implementor {public: Virtual void operation () = 0 ;}; // implement Aclass concreteimplementora: public implementor {public: void operation () {cout <"execute the method in implementation a" <Endl ;}; // implement the bclass concreteimplementorb: Public implementor {public: void operation () {cout <"execute the method in implementation B" <Endl ;}}; // abstract class operator action {public: void setimplementor (impl Ementor * I) {implementor = I;} virtual void operation () = 0 ;~ Export action () {Delete implementor;} protected: implementor * implementor; // contains an Implementation}; // extracted abstract class refine1_action: Public writable action {public: void operation () {implementor-> operation () ;}; int main () {Response Action * AB = new refine1_action (); AB-> setimplementor (New concreteimplementora ()); AB-> operation (); AB-> setimplementor (New concreteimplementorb (); AB-> operation (); // do not forget to delete the pointer Delete AB; system ("pause"); Return 0 ;}
Running result:
The preceding example implements the separation of abstract aggregate action and implementor. The abstraction contains the implementation. However, the specific implementation (concreteimplementora or concreteimplementorb) is determined by the client. To add an abstraction, you only need to inherit the role action. To add an implementation, You can inherit the implementor. This not only reduces the coupling degree, but also conforms to the open-closed principle, that is, the function expansion does not need to modify the original code, but only needs to add the new code.
Reference: Chapter 22nd of "big talk Design Patterns" Wikipedia