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 ");
}
}
}