Design Mode-bridge patterns)

Source: Internet
Author: User
Design Mode (8): Bridging Mode (Bridge patterns)
 
Bridge patterns)
Definition
Separate the abstract part from its implementation part so that they can all change independently.
Overview
The inheritance relationships of objects are defined at compilation, so the implementation of inheritance from the parent class cannot be changed at runtime. The implementation of a subclass is closely related to its parent class, so that any changes in the implementation of the parent class will inevitably lead to changes in the subclass. When you need to reuse the subclass, if the inherited implementation is not suitable for solving the new problem, the parent class must be overwritten or replaced by other more suitable classes. This dependency limits flexibility and ultimately limits reusability.
The separation of the abstract part and the actual part does not mean that the abstract class is separated from its derived class, because it does not have any meaning. Actually, an abstract class and its derived class are used to implement their own objects.
Bridging Mode(Bridge patterns)Structure chart

Implementor class

Abstract   Class Implementor
{
Public   Abstract   Void Operation ();
}
Concreteimplementora and concreteimplementora classes
Class Concreteimplementora: implementor
{
Public   Override   Void Operation ()
{
Console. writeline (implementation of method );
}
}

ClassConcreteimplementorb: implementor
{
Public Override VoidOperation ()
{
Console. writeline (implementation of Method B );
}
}

Class Invalid action
{
Protected Implementor;
Public   Void Setimplementor (implementor)
{
This . Implementor = Implementor;
}

Public   Virtual   Void Operation ()
{
Implementor. Operation ();
}
}

ClassRefinedabstraction: Invalid action
{
Public Override VoidOperation ()
{
Implementor. Operation ();
}
}

Client implementation:

Static   Void Main ( String [] ARGs)
{
Billing action AB = New Refinedaskaction ();
AB. setimplementor ( New Concreteimplementora ());
AB. Operation ();
}

Note:: The implementation system may have multi-angle classification, and each type of classification may change. This multi-angle separation allows them to change independently, reducing coupling between them.
The abstract class is implemented, and the methods are also implemented. Because they use an aggregation relationship. Every action must implement all the methods in implemntor.

Principles of merging/aggregation multiplexing: Use merging/aggregation whenever possible, and do not use inheritance whenever possible.

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.