Design Pattern Bridge Design mode
The bridge design pattern is actually a simple has a relationship, that is, one class has another class and uses another class to implement the required functions.
For example, you can use the bridge design mode between the remote control and the TV to control multiple TVs using the same remote control.
Such a design idea is the basic idea of using multiple design patterns in turn.
After careful consideration, we will find that the underlying ideas of multiple design modes are actually the same, but there are some differences in implementation or some details and applications.
Next we will implement a TV and remoter class, in which remoter can be changed at any time.
#include
class Remoter{public:virtual void changeChannel() = 0;};class OldRemoter : public Remoter{short channel;public:OldRemoter(short c) : channel(c) {}void changeChannel(){printf("Channel : %d\n", channel++);}};class NewRemoter : public Remoter{int channel;public:NewRemoter(int c) : channel(c) {}void changeChannel(){printf("Channel : %d\n", channel++);}};class TV{protected:Remoter *remoter;int channel;public:TV(Remoter *r) : remoter(r), channel(0) {}virtual void changeRemoter(Remoter *r){remoter = r;}virtual void changeChannel(){remoter->changeChannel();}};class BrandOneTV : public TV{public:BrandOneTV(Remoter *r) : TV(r){}};int main(){Remoter *ore = new OldRemoter(0);Remoter *nre = new NewRemoter(1);TV *tv1 = new BrandOneTV(ore);tv1->changeChannel();ore->changeChannel();tv1->changeChannel();tv1->changeRemoter(nre);tv1->changeChannel();nre->changeChannel();tv1->changeChannel();return 0;}
Run: