Design Mode C ++ implementation (10) -- Bridging Mode

Source: Internet
Author: User
Tags install window

The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article introduces the implementation of the bridge mode.

[DP] The book defines that abstract parts are separated from their implementations so that they can all change independently. Consider installing an operating system. There are multiple types of computers and multiple operating systems. How to use the bridge mode? The operating systems and computers can be abstracted separately to let them develop and reduce their coupling. Of course, there is a standard interface between the two. This design is very beneficial to both computers and operating systems. The following shows the UML diagram of this design, which is actually a UML diagram of the bridge mode.

An Implementation of C ++ is provided:

// Operating system class OS {public: Virtual void installos_imp () {}}; class windowos: Public OS {public: void installos_imp () {cout <"Install window OS" <Endl ;}}; class linuxos: Public OS {public: void installos_imp () {cout <"install linux OS" <Endl ;}}; class unixos: Public OS {public: void installos_imp () {cout <"Install Unix OS" <Endl ;}}; // computer class computer {public: Virtual void installos (OS * OS) {}}; class dellcomputer: public Computer {public: void installos (OS * OS) {OS-> installos_imp () ;}}; class applecomputer: Public Computer {public: void installos (OS * OS) {OS-> installos_imp () ;}}; class hpcomputer: Public Computer {public: void installos (OS * OS) {OS-> installos_imp ();}};

Customer usage:

int main(){OS *os1 = new WindowOS();OS *os2 = new LinuxOS();Computer *computer1 = new AppleComputer();computer1->InstallOS(os1);computer1->InstallOS(os2);}

I enjoy the copyright of blog articles, reprint please indicate the source


Related Article

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: 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.