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)!