Bridge (BRIDGE) 3 (typical structure diagram) structural pattern of Design Pattern

Source: Internet
Author: User

Bridge is one of the complex and hard-to-understand modes in the design model, and is also one of the frequently used modes in OO development and design. The combination of abstraction and implementation is completely decoupled. The advantage is that abstraction and implementation can be changed independently, and the coupling of the system is also greatly reduced.

When explaining the Bridge pattern, gof points out that "separating the abstract part from its implementation part allows them to change independently", which is simple but complex. The reason is how to understand "Implementation" in the gof sentence: "Implementation", especially when we put it together with "abstraction", our default understanding is that "Implementation" is the implementation of specific sub-classes of "abstraction, however, the so-called "Implementation" of gof here does not refer to the implementation of virtual functions (interfaces) in the abstract base class by specific subclasses of the abstract base class, but is combined with inheritance.

The meaning of "Implementation" here refers to how to implement the user's needs, and refers to the implementation through combination. Therefore, the implementation here does not refer to the inheritance base class, to implement a base-class interface, you can use object combinations to meet your needs. By understanding this, you can understand the bridge mode.

In fact, the fundamental difference between the use of the brigde mode and the use of the problem solution is whether to implement a functional requirement through inheritance or combination, therefore, the principle of object-oriented analysis and design is favor comosition over inheritance.

 

Brideg mode typical structure

-------------------------------------- Action. h ----------------------------------------------------

# Ifndef _ DEFINE action_h _

# DEFINE _ DEFINE action_h _

 

Class extends actionimp;

 

Class program action

{

Public:

Virtual ~ Invalid action ();

Virtual void operation () = 0;

Protected:

Invalid action ();

};

 

Class refinedabstraction: public: Invalid action

{

Public:

Refinedaskaction (required actionimp * IMP );

~ Refinedaskaction ();

Void operation ();

PRIVATE:

Required actionimp * m_imp;

};

 

# Endif

 

 

------------------------------------- Initiate action. cpp --------------------------------

# Include "invalid action. H"

# Include "Export actionimp. H"

# Include <iostream>

 

Using namespace STD;

 

Required action: required action ()

{}

 

Export Action ::~ Invalid action ()

{}

 

Refined1_action: refined1_action (required actionimp * IMP)

{

M_imp = imp;

}

 

Refinedaskaction ::~ Refinedaskaction ()

{}

 

Void refined1_action: Operation ()

{

Imp-> operation ();

}

 

-------------------------------------- Export actionimp. h -----------------------------------

# Ifndef _ DEFINE actionimp_h _

# DEFINE _ DEFINE action_h _

 

Class program actionimp

{

Public:

Virtual ~ Abstractionimp ();

Virtual void operaction () = 0;

Protected:

Abstractionimp ();

};

 

 

Class concreate1_actionimpa: Public writable actionimp

{

Public:

Concreate?actionimpa ();

~ Concreate?actionimpa ();

Virtual void operation ();

};

 

 

Class concreate?actionimpb: Public writable actionimp

{

Public:

Concreate?actionimpb ();

~ Concreate?actionimpb ();

Virtual void operation ();

};

 

 

-------------------------------------------- Pull actionimp. cpp -------------------------------

# Include "Export actionimp. H"

# Include <iostream>

Using namespact STD;

 

Required actionimp: Required actionimp ()

{}

 

Required actionimp ::~ Abstractionimp ()

{}

 

Void merge actionimp: Operation ()

{

Cout <"Export actionimp... imp..." <Endl;

}

 

Concreate?actionimpa: concreate?actionimpa ()

{}

 

Concreate?actionimpb ::~ Concreate?actionimpa ()

{}

 

Void concreate1_actionimpa: Operation ()

{

Cout <"concreate?actionimpa..." <Endl;

}

 

Concreate?actionimpb: concreate?actionimpb ()

{}

 

Concreate?actionimpb ::~ Concreate?actionimpb ()

{}

 

Void concreate?actionimpb: Operation ()

{
Cout <"concreate?actionimpb..." <Endl;

}

 

----------------------------------------- Main. cpp ---------------------------------------

# Include "invalid action. H"

# Include "Export actionimp. H"

 

# Include <iostream>

Using namespact STD;

 

Int main (INT argc, char * argv [])

{

Required actionimp * imp = new concreate?actionimpa ();

Required action * ABS = new refined1_action (IMP );

Abs-> operation ();

Return 0;

 

}

 

 

 

 

 

 

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.