Analysis, structure diagram, and Basic Code of the Bridge Mode

Source: Internet
Author: User

Analysis, structure diagram, and Basic Code of the Bridge Mode
Zookeeper

We discussed the visitor mode yesterday. Today we will talk about the Bridge Mode:

Definition: separates abstract parts from their implementations, so that they can change independently.
This does not mean that the abstract class is separated from its derived class because it does not make any sense. Implementation refers to the abstract class and its derived class used to implement their own objects.
Understanding: 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 following describes the principles of synthesis/aggregation multiplexing:
The principle of merging/aggregation reuse is to use synthesis/aggregation as much as possible, and do not use class inheritance as much as possible. Aggregation indicates A weak 'Owning 'relationship. It indicates that object A can contain object B, but object B is not part of object; synthesis is a strong 'havn' relationship, reflecting the strict relationship between the part and the whole, and the same as the overall life cycle. Preferential use of object merging/aggregation will help you keep each class encapsulated and concentrated on a single task. In this way, the Class and Class inheritance layers will maintain a small scale and are unlikely to grow into an uncontrollable giant.
Applicability: if we find that there are two or more changes in the inheritance system, we can decouple these changes in different directions. Through object combination, the inheritance relationship between the two roles is changed to the combination relationship, so that the two can cope with their independent changes. Find and encapsulate the changes in one sentence ".
Structure:


Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace Bridge Mode
{
Class Program
{
Static void Main (string [] args)
{
Required action AB = new refined1_action ();

AB. SetImplementor (new ConcreteImplementorA ());
AB. Operation ();

AB. SetImplementor (new ConcreteImplementorB ());
AB. Operation ();

Console. Read ();
}
}

Class program action
{
Protected Implementor implementor;

Public void SetImplementor (Implementor implementor)
{
This. implementor = implementor;
}

Public virtual void Operation ()
{
Implementor. Operation ();
}

}

Class RefinedAbstraction: Invalid action
{
Public override void Operation ()
{
Implementor. Operation ();
}
}

Abstract class Implementor
{
Public abstract void Operation ();
}

Class ConcreteImplementorA: Implementor
{
Public override void Operation ()
{
Console. WriteLine ("Implementation of method ");
}
}

Class ConcreteImplementorB: Implementor
{
Public override void Operation ()
{
Console. WriteLine ("Implementation of Method B ");
}
}

}

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.