Home > Backend Development > C#.Net Tutorial > A brief introduction to bridge mode in C++ design patterns

A brief introduction to bridge mode in C++ design patterns

黄舟
Release: 2017-01-17 13:45:27
Original
1274 people have browsed it

Composition/Aggregation Reuse Principle (CARP):

Prefer using composition/aggregation instead of using class inheritance.

Aggregation represents a weak "ownership relationship", which means that object A can contain object B, but object B is not part of object A.

Synthesis represents a strong "ownership relationship", reflecting the strict relationship between parts and wholes, and the life cycles of parts and wholes are the same.

Benefits: Prioritizing composition/aggregation of objects will help you keep each class encapsulated and focused on a single task. This way classes and class inheritance hierarchies stay small and are less likely to grow into the uncontrollable behemoth that inheritance can.

Inheritance is a strongly coupled structure. If the parent class changes, the subclass will also change. When using inheritance, be sure to consider using it when it is an ‘is-a’ relationship, rather than using it at any time.

Bridge mode (Bridge): Separates the abstract part from its implementation part so that they can change independently.

Implementation refers to the abstract class and its derived classes used to implement their own objects.

Test case:

[code]int main(){
    ConerectImplementorA *A = new ConerectImplementorA;
    ConerectImplementorB *B = new ConerectImplementorB;

    Abstraction *abs = new Abstraction;
    abs->setImplementor(A);
    abs->Operation();  //Output: ConerectImplementorA.

    Abstraction *abs2 = new Abstraction;
    abs2->setImplementor(B);
    abs2->Operation();  //Output: ConerectImplementorB.

    return 0;
}
Copy after login

Pattern implementation

[code]//Implementor类,实现类
class Implementor{
public:
    virtual void Operator(){      
    }
};

//具体实现类A
class ConerectImplementorA: public Implementor{
    virtual void Operator(){
        std::cout << "ConerectImplementorA.\n";
    }  
};

//具体实现类B
class ConerectImplementorB: public Implementor{
    virtual void Operator(){
        std::cout << "ConerectImplementorB.\n";
    }
};

//抽象类->桥接Implementor类
class Abstraction{
protected:
    Implementor *imp;    
public:
    void setImplementor(Implementor *imp){
        this->imp = imp;
    }
    virtual void Operation(){
        imp->Operator();
    }
};

//被提炼的对象
class RefinedAbstraction: public Abstraction{
    virtual void Operation()override{
        imp->Operator();
    }
};
Copy after login

Summary: The implementation system may be classified from multiple angles, and each category may change, so separate these multiple angles and let them change independently, reducing the differences between them of coupling.

The above is the content of the C++ design pattern brief introduction to the bridge mode. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template