Bridging mode:
Separate the abstract and its implementation parts so that they can vary individually.
UML diagram:
Mainly include:
- Abstraction: An interface that defines an abstract part that operates a reference to an implementation part of an object.
- Refinedabstraction: A class that inherits from the abstract part.
- Implementor: Implements the part of the interface.
- Concreteimplementor: Implements a specific class of interfaces defined by Implementor.
The C + + code is as follows:
#include <iostream>usingNamespace Std;class implementor{ Public:Virtual void Operationimpl()=0;}; Class Concreteimplementora: Publicimplementor{ Public:void Operationimpl() {cout<<"Concreteimplementora::operationimpl"<<endl; }};class Concreteimplementorb: Publicimplementor{ Public:void Operationimpl() {cout<<"Concreteimplementorb::operationimpl"<<endl; }};class abstraction{ Public:Virtual void Operation()=0;voidSetimplementor (Implementor * i) {impl=i; } implementor * Getimplementor () {returnImpl }protected: Implementor * IMPL; };class refinedabstraction: Publicabstraction{ Public:void Operation() {Impl->operationimpl (); }};intMain () {cout<<"Bridging mode Example"<<endl; Abstraction * ab=NewRefinedabstraction (); Implementor * cia=NewConcreteimplementora (); Ab->setimplementor (CIA); Ab->operation (); Implementor * cib=NewConcreteimplementorb (); Ab->setimplementor (CIB); Ab->operation (); Delete CIA; Delete CIB; Delete ab;return 0;}
Execution output:
Here is a concrete example of this specific example that may well be understood, excerpted from the Big Talk design pattern:
- Abstraction is the phone (mobile).
- Refinedabstraction is Samsung (Samsung mobile), (Mobile).
- Implementor for Game (mobile games).
- Concreteimplementor for Needforspeed (need for Speed), Qqgame (QQ game), Fruitninjia (Fruit Ninja).
UML Class diagrams are:
C + + code:
#include <iostream>using namespace STD;classgame{ Public:Virtual voidPlay () =0;};classNeedforspeed: Publicgame{ Public:Virtual voidPlay () {cout<<"Need for Speed play"<<endl; }};classQqgame: Publicgame{ Public:Virtual voidPlay () {cout<<"Qqgame Play"<<endl; }};classFruitninjia: Publicgame{ Public:Virtual voidPlay () {cout<<"Fruit Ninjia Play"<<endl; }};classphone{ Public:Virtual voidRun () =0;voidSetgame (Game *g) {game=g; } Game * Getgame () {returnGame }protected: Game *game;};classSamsung: Publicphone{ Public:Virtual voidRun () {cout<<"Samsung:"; Game->play (); }};class: Publicphone{ Public:Virtual voidRun () {cout<< Game->play (); }};intMain () {cout<<"Bridging mode real examples, different mobile phone brands and mobile games"<<endl; Phone *samsung=NewSamsung (); Phone *=New (); Game * needforspeed=NewNeedforspeed (); Game * qqgame=NewQqgame (); Game * fruit=NewFruitninjia (); Samsung->setgame (Qqgame); Samsung->run (); ->setgame (Needforspeed); ->run (); Samsung->setgame (fruit); Samsung->run ();DeleteSamsungDeleteDeleteNeedforspeed;DeleteQqgame;DeleteFruitreturn 0;}
Execution output:
Design mode 18: Bridging mode (bridge)