C + + design mode shallow knowledge bridge mode

Source: Internet
Author: User
Synthesis/Aggregation multiplexing principle (CARP):

Instead of using class inheritance, the composition/aggregation is preferred.

Aggregation represents a weak "owning relationship", which reflects that a object can contain a B object, but the B object is not part of a object.

Synthesis represents a strong "ownership relationship" that embodies a strict part-to-whole relationship, as part of the life cycle of the whole.

Its benefits: prioritizing the composition/aggregation of objects will help you keep each class encapsulated and focused on a single task. Such class and class inheritance hierarchies remain small and are unlikely to grow into uncontrollable behemoths, and inheritance is possible.

Inheritance is a strongly coupled structure in which the parent class changes and the subclass changes. When using inheritance, be sure to consider using it when it's a ' is-a ' relationship, rather than using it at any time.

Bridging mode (bridge): separates the abstract part from its implementation, so that both can change independently.

An implementation refers to an abstract class and its derived classes that are 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;}

Pattern implementation

[Code]//implementor class, Implementing Class Class Implementor{public:    virtual void Operator () {          }};//Concrete implementation class AClass Conerectimplementora:public implementor{    virtual void Operator () {        std::cout << "Conerectimplementora. \ n ";    }  };/ /Concrete Implementation class Bclass Conerectimplementorb:public implementor{    virtual void Operator () {        std::cout << " Conerectimplementorb.\n ";    }};/ /abstract Class-Bridge Implementor class abstraction{protected:    implementor *imp;    Public:    void Setimplementor (Implementor *imp) {        this->imp = imp;    }    virtual void operation () {        imp->operator ();    }};/ /Refined Object class Refinedabstraction:public abstraction{    virtual void operation () override{        imp->operator () ;    }};

Summary: The implementation of the system may be multi-angle classification, each classification is likely to change, then the multi-angle separation to allow them to change independently, reducing the coupling between them.

The above is the C + + design mode shallow knowledge bridge mode content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.