Design patterns to achieve the two languages (C ++ and Java) (IX: bridge mode)

When designing the class attribute has multiple dimensions, the design will cause a lot of design subclass, classification difficulties with single inheritance way.

Abstract and bridge mode separation, so that they may be varied independently. It is achieved by a combination relationship instead of inheritance, and abstraction implemented to reduce the dimensions of the two variable degree of coupling.

Bridge mode features:

1. expansion capability, to achieve the separation and inheritance.

2. The implementation details transparent to the client.

Consider the operating system installed, there are a variety of computer configurations, there are also a variety of operating systems. How to use bridge mode it? That the operating system and the computer are abstracted, so that their respective development, reduce their degree of coupling. Of course, standard interface between the two. This design, whether it is for a computer or operating system are very favorable.

 

 

C ++ implementation:

. 1 #include <the iostream>
 2  
. 3  the using  namespace STD;
 . 4  
. 5  // operating systems 
. 6  class the OS
 . 7  {
 . 8  public :
 . 9      Virtual  void InstallOS_Imp ()} {
 10  };
 . 11  class WindowOS: public the OS
 12 is  {
 13 is  public :
 14      void InstallOS_Imp () {COUT << " installation Window operating system " << endl;}
 15  };
 16  classLinuxOS: public the OS
 . 17  {
 18 is  public :
 . 19      void InstallOS_Imp () {COUT << " install Linux " << endl;}
 20 is  };
 21 is  class UnixOS: public the OS
 22 is  {
 23 is  public :
 24      void InstallOS_Imp () {COUT << " installation Unix operating system " << endl;}
 25  };
 26 is  // computer 
27  class computer
 28  {
 29  public:
 30      virtual  void install (OS * os)} {
 31  };
32  class DellComputer: Public Computer
 33  {
 34  public :
 35      void install (the OS *) {os> InstallOS_Imp (); }
 36  };
37  class AppleComputer: Public Computer
 38  {
 39  public :
 40      void install (the OS *) {os> InstallOS_Imp (); }
 41  };
42  class HPComputer: public Computer
43 {
44 public:
45     void InstallOS(OS *os) { os->InstallOS_Imp(); }
46 };
47 
48 int main()
49 {
50     cout << "Hello World!" << endl;
51     return 0;
52 }

Java implementation:

Guess you like

Origin www.cnblogs.com/Asp1rant/p/10923744.html