Design mode C + + description----09. Bridging (bridge) mode __c++

Source: Internet
Author: User

I. Examples

N years ago:

When the computer first comes out, software and hardware are tied together, such as IBM out of a computer, there is a custom system, if called Ibm_win, this ibm_win system of course can not run on HP computers, the same HP out of the Hp_win system can not run on IBM.

If a new company, Apple, had a new computer, the company would also develop its own system apple_win,apple_linux.

In fact, the software and hardware developed by several companies are similar in many places, and if each company is doing its own system, it means a huge waste of resources and people.

At this point, regardless of the emergence of new systems, or the emergence of new companies, this design pattern changes are very large.

n years later:

Windows appears, Linux appears, and software and hardware are finally separated. Systems such as Windows can run on IBM machines as well as on HP machines.

The software is focused on the software and the hardware is focused on the hardware. That's why the computer system has been growing (more focused) and the reason it's driving more companies (less expensive).

Whether it's a new hardware platform or a new OS, everyone can focus on their own domain. That is, the external changes are very large, but the impact on this model is very small.

Code implementation:

Operating system
class OS  
{public  
:  
    virtual void Run () {}  
};

Windows
class Windowos:public OS  
{public
:
    void Run () {cout<< run Windows <<endl;}   
} ;

Linux
class Linuxos:public OS  
{public
:
    void Run () {cout<< "Running 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;
}

Two. Bridging mode

Definition: separates the abstract part from its implementation, so that they can all change independently .

In other words, it is to make the implementation independent, so that they change, without affecting other implementations, so as to reduce the coupling.

Why is it called bridge mode, as can be seen from the above figure, it is like a bridges, the bridge on both sides of the system is relatively independent, the left is an abstract part, the right is the implementation part.

three. Combinatorial, aggregate reuse principle


From the above two kinds of structural graphs, we can draw a principle: try to use composition, aggregation, try not to use class inheritance.


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.