C + + Bridging mode __c++

Source: Internet
Author: User
Tags switches
Brief

Bridging pattern is the separation of the abstract part from its implementation, so that they can be changed independently.

Brief description of the advantages and disadvantages of pattern structure Application scenario case Analysis Code implementation

All rights reserved: A go, two or three, reprint please specify the Source: http://blog.csdn.net/liang19890820 mode structure

UML structure diagram:

Abstraction (abstract Class): An interface that defines an abstract class and maintains a pointer to the Implementor implementation class. It has an association with implementor. Refinedabstraction (Extended abstract Class): Expands the interface defined by abstraction, in which the business methods defined in Implementor can be invoked. Implementor (Implementation class Interface): Defines the interface of the implementation class, which does not necessarily correspond exactly to the abstraction interface, in fact the two interfaces can be completely different. Concreteimplementor (Concrete implementation Class): Implements the interface defined by Implementor, provides different implementations of basic operations in different concreteimplementor. When the program is run, the Concreteimplementor object replaces its parent class object and provides abstraction specific business action methods. Pros and cons

Advantages: Separation of abstraction and implementation parts. Bridging mode uses an "association relationship between objects" to decouple the intrinsic binding relationship between abstraction and implementation, allowing abstractions and implementations to evolve along their respective dimensions. The so-called abstraction and implementation change along the respective dimensions, that is, the abstraction and implementation are no longer in the same inheritance hierarchy, but "subclasses" of them, so that they each have their own subclasses, so that any combination of subclasses, so as to obtain a multidimensional combination of objects. In many cases, bridge mode can replace the multi-level inheritance scheme, the multi-level inheritance scheme violates the "single responsibility Principle", the reusability is poor, and the number of classes is very large, bridge mode is a better solution than multi-layer inheritance scheme, it greatly reduces the number of subclasses. Bridging mode improves the scalability of the system, and expands a dimension in two change dimensions without modifying the original system, which conforms to the "opening and closing Principle".

Disadvantage: The use of bridging mode will increase the system's understanding and design difficulty, because the association relationship is based on the abstraction layer, requires the developer to start with the abstraction layer design and programming. Bridge mode requires the correct identification of two independent dimensions of the system, so its scope of use has certain limitations, how to correctly identify two independent dimensions also need some experience accumulation. applicable Scenarios if a system needs to add more flexibility between abstraction and materialization, avoid establishing static inheritance relationships between two levels, the bridging pattern allows them to establish an association relationship at the abstraction level. The "abstract" and "implementation" parts can be extended independently of each other in an inherited way, and the object of an abstract subclass can be dynamically combined with an object that implements the subclass when the program is run, that is, the system needs to dynamically decouple the abstract role and the implementation role. A system has multiple (≥2) independently changing dimensions, and these dimensions need to be extended independently. Bridge mode is especially useful for systems that do not want to use inheritance or that cause multiple levels of system classes to increase dramatically. Case Analysis

Switches and Appliances

Electrical appliances are essential to modern life, almost every household have, television, fans, electric lights ... No matter what electrical appliances, are controlled by the switch. Many types of switches, zipper-type switch, two-bit switch, dimming switch ...

You can change the device at any time without touching anything else. For example, you can change the bulb without changing the switch, or you can change the switch without touching the bulb or fan, or even swap the bulb and the fan without touching the switch.

This seems natural, of course. When different things are linked together, they should be in a system that can be changed or replaced so that they do not interact or affect as little as possible, so that the system can be managed more easily and cheaply. Imagine, if you want to change a light bulb in the room, you need to change the switch, you will consider using such a system. Code Implementation

To create an implementation class interface

All appliances have some commonality that can be turned on and off:

Implementor.h
#ifndef implementor_h
#define IMPLEMENTOR_H

//Electrical
class Ielectricalequipment
{ Public
:
    virtual ~ielectricalequipment () {}

    //Open
    virtual void PowerOn () = 0;

    Close
    virtual void poweroff () = 0;
};

#endif//Implementor_h

To create a concrete implementation class

Next, the real appliances--the lights and the fans--they implement the Ielectricalequipment interface:

Concrete_implementor.h
#ifndef concrete_implementor_h
#define CONCRETE_IMPLEMENTOR_H

#include " Implementor.h "
#include <iostream>

//Lighting
class Light:public ielectricalequipment
{
Public:
    //Open light
    virtual void PowerOn () override {
        Std::cout << "Light is on" << Std::endl;
    }

    //Turn off light
    virtual void Poweroff () override {
        Std::cout << "Light is off." << Std::endl;
    }
};

Fan
class Fan:public ielectricalequipment
{public
:
    //Open fan
    virtual void PowerOn () Override {
        Std::cout << "Fan is on." << Std::endl;
    }

    Turn off fan
    virtual void Poweroff () override {
        Std::cout << "Fan is off." << Std::endl;
    }
};

#endif//Concrete_implementor_h

To create an abstract class

For switches, it does not know the existence of lights and fans, only know that they can control (turn on/off) an electrical appliance. In other words, each iswitch should hold a Ielectricalequipment object:

Abstraction.h
#ifndef abstraction_h
#define Abstraction_h

#include "implementor.h"

//Switch
Class Iswitch
{public
:
    iswitch (ielectricalequipment *ee) {m_pee = EE;}
    Virtual ~iswitch () {}

    //Open appliance
    virtual void on () = 0;

    Turn off the appliance
    virtual void off () = 0;

Protected:
    ielectricalequipment *m_pee;

#endif//Abstraction_h

To create an extended abstract class

A number of specific types of switches, such as zipper-type switch, two-bit switch:

Refined_abstraction.h #ifndef refined_abstraction_h #define REFINED_ABSTRACTION_H #include "abstraction.h" #include <iostream>//Zipper type switch class pullchainswitch:public Iswitch {public:pullchainswitch (ielectricalequipment *ee) : Iswitch (ee) {}///Open electrical appliance with zipper switch virtual void on () override {std::cout << switch on the equipment W
        ITH a pull chain switch. "<< Std::endl;
    M_pee->poweron (); ////zip switch turn off appliance virtual void off () override {Std::cout <<] switch off the equipment with a pull ch
        Ain switch. "<< Std::endl;
    M_pee->poweroff ();

}
}; Two-bit switch class Twopositionswitch:public Iswitch {public:twopositionswitch (ielectricalequipment *ee): ISwitch (ee) { ///With two-bit switch to open electrical appliance virtual void on () override {std::cout << "switch on the equipment with a Two-positi
        On switch. "<< Std::endl;
    M_pee->poweron (); ///With two-bit switch turn off the appliance virtual void off () override {
        Std::cout << "switch off the equipment with a two-position switch." << Std::endl;
    M_pee->poweroff ();

}
}; #endif//Refined_abstraction_h

Creating a client

Well, it's time to associate the switch with the appliance:

Main.cpp
#include "refined_abstraction.h"
#include "concrete_implementor.h"

#ifndef safe_delete
#define SAFE_DELETE (p) {if (p) {DELETE (P); (p) =null;}
#endif

int main ()
{
    //create electrical appliances-lights, fan
    ielectricalequipment *light = new Light ();
    Ielectricalequipment *fan = new Fan ();

    /**
     * Create switch-zipper-type switch, two-bit switch
     * Associate the zipper switch with the lamp, associate the two-bit switch with the fan
    **/
    iswitch *pullchain = new Pullchainswitch ( Light);
    Iswitch *twoposition= new Twopositionswitch (fan);

    Turn on the light, turn off the light
    pullchain->on ();
    Pullchain->off ();

    Turn on the fan, turn off the fan
    Twoposition->on ();
    Twoposition->off ();

    Safe_delete (twoposition);
    Safe_delete (pullchain);
    Safe_delete (fan);
    Safe_delete (light);

    GetChar ();

    return 0;
}

The output is as follows:

The

switch on the equipment with a pull chain switch.
Light is on.
Switch off the equipment with a pull chain switch.
Light is off. The
switch on the equipment with a two-position switch.
Fan is on.
Switch off the equipment with a two-position switch.
Fan is off.

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.