Design Mode (7)-structural mode-Bridge Mode

Source: Internet
Author: User

2. Structural Mode 2.2 BRIDGE Mode

Alias: handle/body

This mode reflects the advantages of a combination over inheritance.

2.2.1 motivation

When an abstraction may have multiple implementations, inheritance is usually used to coordinate them. The abstract class defines the abstract interface, and the specific subclass is implemented in different ways. However, this method is sometimes not flexible enough. The Inheritance Mechanism binds the abstract Part with its implementation part, making it difficult to modify, expand, and reuse the abstract Part and implementation part independently. The combination method allows independent modification through the abstract part and the implementation part.

2.2.2 Structure

Client
-Users in Bridge Mode

? Invalid action

-Define the abstract class interface.

-Maintain a pointer to an Im p l e m e n t o r object. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + expires/expires + expires/2r/expires/avfbm4bmp U/mxvrLZ1/expires/nT2tXi0Km7 + bG + stnX97XEvc + expires/expires + expires/LXEvt/ examples/examples + nP88DgtcTKtc/examples + examples/examples + MikgIMzhuN +/ycCps + TQ1CDE47/examples + examples/cgxOO/ydLUttS/examples/PS1Lywz + examples + CjxoMz4yLjIuNCDA/logs + signature + ezwvcD4KPHA + signature + fTs8L3A + signature + cHJpdmF0ZTo8L3A + signature + Signature + CjxwPiA8L3A + CjxwPi8vQWJzdHJhY3Rpb24uY3BwPC9wPgo8cD4jaW5jbHVkZQ = "invalid action. h"

# Include "Export actionimp. h"

# Include

Using namespace std;

Required action: required action ()

{

}

Export Action ::~ Invalid action ()

{

}

Refinedmediaaction: refinedmediaaction (lifecycle

CtionImp * imp)

{

_ Imp = imp;

}

Refinedaskaction ::~ Refinedaskaction ()

{

}

Void refined1_action: Operation ()

{

_ Imp-> Operation ();

}

Code snippet 3: Export actionimp. h

// Define actionimp. h

# Ifndef _ DEFINE actionimp_h _

# Define _ define actionimp_h _

Class program actionimp

{

Public:

Virtual ~ AbstractionImp ();

Virtual voidOperation () = 0;

Protected:

AbstractionImp ();

Private:

};

Class concrete=actionimpa: public

AbstractionImp

{

Public:

Concrete=actionimpa ();

~ Concrete=actionimpa ();

Virtual voidOperation ();

Protected:

Private:

};

Class ConcreteAbstractionImpB: public

AbstractionImp

{

Public:

Concrete?actionimpb ();

~ Concrete?actionimpb ();

Virtual voidOperation ();

Protected:

Private:

};

# Endif //~ _ Required actionimp_h _

Code snippet 4: Export actionimp. cpp

// Define actionimp. cpp

# Include "Export actionimp. h"

# Include

Using namespace std;

Required actionimp: Required actionimp ()

{

}

Required actionimp ::~ AbstractionImp ()

{

}

Void merge actionimp: Operation ()

{

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

}

Concrete?actionimpa: concrete=actio

NImpA ()

{

}

Concrete=actionimpa ::~ ConcreteAbstracti

OnImpA ()

{

}

Void concrete=actionimpa: Operation ()

{

Cout <"concrete?actionimpa..." <e

Ndl;

}

Concrete?actionimpb: concrete=actio

NImpB ()

{

}

Concrete?actionimpb ::~ ConcreteAbstracti

OnImpB ()

{

}

Void concrete?actionimpb: Operation ()

{

Cout <"concrete?actionimpb..." <e

Ndl;

}

Code snippet 5: main. cpp

// Main. cpp

# Include "invalid action. h"

# Include "Export actionimp. h"

# Include

Using namespace std;

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

{

Required actionimp * imp = new

Concrete=actionimpa ();

Define action * abs = new

Refinedaskaction (imp );

Abs-> Operation ();

Return 0;

}

2.2.5 example-JAVA

Implementor class:

1.PackageCom. qianyan. bridge;

2.

3.Public interfaceEngine {

4.

5 ./**

6. * install and launch the engine

7 .*/

8.Public voidInstallEngine ();

9 .}

ConcreteImplementor class:

1.PackageCom. qianyan. bridge;

2.

3.Public classEngine2000ImplementsEngine {

4.

5. @ Override

6.Public voidInstallEngine (){

7. System. out. println ("2000CC engine installation ");

8 .}

9.

10 .}

1.PackageCom. qianyan. bridge;

2.

3.Public classEngine2200ImplementsEngine {

4.

5. @ Override

6.Public voidInstallEngine (){

7. System. out. println ("installing the 2200CC engine ");

8 .}

9.

10 .}

Invalid action class:

1.PackageCom. qianyan. bridge;

2.

3.Public abstract classVehicle {

4.

5.PrivateEngine engine;

6.

7.PublicVehicle (Engine engine ){

8.This. Engine = engine;

9 .}

10.

11.PublicEngine getEngine (){

12.ReturnEngine;

13 .}

14.

15.Public voidSetEngine (Engine engine ){

16.This. Engine = engine;

17 .}

18.

19.Public abstract voidInstallEngine ();

20 .}

Refined Response Action class:

1.PackageCom. qianyan. bridge;

2.

3.Public classBusExtendsVehicle {

4.

5.PublicBus (Engine engine ){

6.Super(Engine );

7 .}

8.

9. @ Override

10.Public voidInstallEngine (){

11. System. out. print ("Bus :");

12.This. GetEngine (). installEngine ();

13 .}

14.

15 .}

1.PackageCom. qianyan. bridge;

2.

3.Public classJeepExtendsVehicle {

4.

5.PublicJeep (Engine engine ){

6.Super(Engine );

7 .}

8. @ Override

9.Public voidInstallEngine (){

10. System. out. print ("Jeep :");

11.This. GetEngine (). installEngine ();

12 .}

13.

14 .}

1.PackageCom. qianyan. bridge;

2.

3.Public classMainClass {

4.Public static voidMain (String [] args ){

5. Engine engine2000 =NewEngine2000 ();

6. Engine engine2200 =NewEngine2200 ();

7.

8. Vehicle bus =NewBus (engine2000 );

9. bus. installEngine ();

10.

11. Vehicle jeep =NewJeep (engine2200 );

12. jeep. installEngine ();

13 .}

14 .}

Result:

1. Bus: Install 2000CC Engine

Jeep: Install the 2200CC Engine

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.