Design mode 18: Bridging mode (bridge)

Source: Internet
Author: User



Bridging mode:
Separate the abstract and its implementation parts so that they can vary individually.



UML diagram:



Mainly include:


    1. Abstraction: An interface that defines an abstract part that operates a reference to an implementation part of an object.
    2. Refinedabstraction: A class that inherits from the abstract part.
    3. Implementor: Implements the part of the interface.
    4. 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:


    1. Abstraction is the phone (mobile).
    2. Refinedabstraction is Samsung (Samsung mobile), (Mobile).
    3. Implementor for Game (mobile games).
    4. 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)


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.