[Design mode] Bridge Mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.