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 http://blog.csdn.net/wuzhekai1985