Design Pattern Bridge Design mode

Source: Internet
Author: User

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:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.