First, UML diagram
Second, the concept
Bridging Mode (bridge): separates the abstract part from its implementation, allowing them to change independently.
Third, the description
Why is it called "Bridging mode"?
As shown in the UML diagram, there is an aggregation line, like a bridge, so called "Bridging mode".
Concept Explanation:
What is called abstraction is separate from its implementation, which is not to say that the abstract class is separated from its derived class because it makes no sense. An implementation refers to an abstract class with its derived class used to implement its own object.
Why should I use the composition/aggregation principle first?
(1) An object's inheritance is defined at compile time, so it is no longer possible to change the implementation inherited from the parent class. The implementation of a subclass has a very close dependency on its parent class, so that any changes to the parent class's implementation will inevitably cause the subclass to change. When you need to reuse a subclass, the parent must override or be replaced by another more appropriate class if the inherited implementation is not appropriate to solve the new problem. This dependency limits flexibility and ultimately limits reusability.
(2) preferential use of the composition/aggregation principle helps you keep each class encapsulated and focused on a single task. Such class and class inheritance hierarchies remain small and are unlikely to grow into uncontrollable behemoths.
What is the essence of the problem of blindly using inheritance?
Inheritance is a strongly-coupled structure. When the parent class changes, the subclass must be changed. Therefore, when we use inheritance, we must consider using it when it is a "is-a" relationship, rather than using it at any time.
Iv. implementation of C + +
(1) Bridge.h
#ifndef bridge_h#define bridge_h#include <iostream> #include <string>//implementor: abstract class for mobile software handsetsoft{public:virtual void Run () =0;};/ /concreteimplementora, here is the mobile game class Handsetgame:public handsetsoft{void run () {std::cout<< "running mobile games" <<STD :: endl;}};/ /concreteimplementorb, here is the phone Address Book class Handsetaddresslist:public handsetsoft{void Run () {std::cout<< "Running phone contacts" <<std::endl;}};/ /abstraction, here is the mobile phone brand abstract class handsetbrand{protected:handsetsoft* soft;public:void Sethandsetsoft (HandsetSoft* soft ) {This->soft=soft;} virtual void Run () =0;};/ /refineabstraction, here for mobile phone products NClass handsetbrandn:public handsetbrand{public:void Run () {soft->run ();}};/ /refineabstraction, here for mobile phone product MClass handsetbrandm:public handsetbrand{public:void Run () {Soft->run ();}}; #endif
(2) Client.cpp
#include "Bridge.h" #include <iostream> #include <cstdlib>//client, client void Main () {//Mobile phone brand nstd::cout< < "mobile phone brand N:" <<std::endl; handsetbrand* ab=new Handsetbrandn (); Ab->sethandsetsoft (new Handsetgame ()); Ab->run (); Ab->sethandsetsoft ( New Handsetaddresslist ()); Ab->run ();d elete ab;//mobile phone brand mstd::cout<<std::endl<< "mobile phone brand M:" <<STD:: Endl;ab=new Handsetbrandm (); Ab->sethandsetsoft (new Handsetgame ()); Ab->run (); Ab->sethandsetsoft (new Handsetaddresslist ()); Ab->run ();d elete ab;ab=null;std::cout<<std::endl;system ("pause");}
(3) operation
Big talk design pattern C + + Implementation-22nd chapter-Bridging mode