I. Example
N years ago:
When the computer first came out, the software and hardware were tied together. For example, IBM had a computer with a customized system, for example, ibm_win, of course, this ibm_win system cannot run on an HP computer, and HP's hp_win system cannot run on IBM.
If a new company, apple, has a new computer, needs to develop its own system apple_win and apple_linux.
In fact, the software and hardware developed by the above companies have similarities in many places. If each company has its own system, this means a great waste of resources and personnel.
At this time, no matter the emergence of new systems or new companies, the changes to this design model are very large.
N years later:
Windows, Linux, and hardware are finally separated. For example, Windows can run on both IBM and HP machines.
In this case, the software focuses on the software, and the hardware focuses on the hardware. This is also the reason for the continuous development of computer systems (more focused) and the reason for the emergence of more companies (lower costs ).
Whether it's a new hardware platform or a new OS, everyone can focus on their own fields. That is, the external world is greatly changed, but the impact on this model is very small.
// Operating system class OS {public: Virtual void run () {}}; // windowsclass windowos: Public OS {public: void run () {cout <"run Windows" <Endl ;}; // linuxclass linuxos: Public OS {public: void run () {cout <"run Linux" <Endl ;}; // computer class computer {public: Virtual void installos (OS * OS ){}}; // IBM computer class ibmcomputer: Public Computer {public: void installos (OS * OS) {OS-> Run () ;}}; // HP computer class hpcomputer: public Computer {public: void installos (OS * OS) {OS-> Run () ;}}; int main () {OS * os1 = new windowos (); OS * os2 = new linuxos (); computer * computer1 = new ibmcomputer (); computer1-> installos (os1); computer1-> installos (os2); Return 0 ;}
Ii. Bridging Mode
Definition:Separates abstract parts from their implementations so that they can all change independently..
In other wordsIndependent implementation, so that they change independently without affecting other implementationsTo reduce coupling.
What is the Bridge Mode? It can be seen that it is like a bridge. The systems on both sides of the bridge are relatively independent, the abstract part on the left and the implementation part on the right.
Iii. Principles of combination and aggregation Reuse
From the comparison of the above two types of structure charts, we can introduce a principle:Try to use combination and aggregation, and try not to use class inheritance.