1. Overview
The Bridge Mode separates the abstract part from its implementation part, so that they can all change independently.It does not mean that abstract classes are separated from other derived classes because it has no meaning. Implementation refers to the abstract class and its derived class used to implement their own objects.
The following figure shows the structure of the bridge mode:
(1). Define action: Define the abstract class interface and maintain the object pointer to the implementor class.
(2). refined1_action: expands the interface defined by the specified action.
(3). implementor: defines the interface of the implementation class. This interface does not have to be exactly the same as the interface of the implementation action. In fact, these two interfaces can be completely different. In general, the implementor interface only provides basic operations, while the role Action defines high-level operations based on these basic operations.
(4). concreteimplementor: implements the implementor interface and defines its specific implementation.
The basic code for the bridge mode is as follows:
Implementor class:
Public abstract class implementor
{
Public abstract void operation ();
}
Concreteimplementor and other derived classes:
Public class concreteimplementora: implementor
{
Public override void operation ()
{
Console. writeline ("Implementation of method ");
}
}
Public class concreteimplementorb: implementor
{
Public override void operation ()
{
Console. writeline ("Implementation of Method B ");
}
}
Invalid action class:
Public class internal action
{
Protected implementor;
Public void setimplementor (implementor)
{
This. implementor = implementor;
}
Public Virtual void operation ()
{
Implementor. Operation ();
}
}
Refinedmediaaction class:
Public class refinedabstraction: Invalid action
{
Public override void operation ()
{
Implementor. Operation ();
}
}
2. Instance
To better understand the bridge model, let's take a look at the evolution of mobile phone software in the big talk design model. Let's take a look at the first version of the Code. The structure is as follows:
The Code is as follows:
// Mobile phone brand
Public class handsetbrand
{
Public Virtual void run ()
{
}
}
// Mobile phone brand m
Public class handsetbrandm: handsetbrand
{
}
// Mobile phone brand N
Public class handsetbrandn: handsetbrand
{
}
// Mobile phone brand M game
Public class handsetbrandmgame: handsetbrandm
{
Public override void run ()
{
Console. writeline ("Running M-branded mobile games ");
}
}
// Game with mobile phone brand N
Public class handsetbrandngame: handsetbrandn
{
Public override void run ()
{
Console. writeline ("Running N-brand Mobile Games ");
}
}
// Address Book of mobile phone brand m
Public class handsetbrandmaddresslist: handsetbrandm
{
Public override void run ()
{
Console. writeline ("Run M-branded mobile phone Address Book ");
}
}
// Address Book of mobile phone brand N
Public class handsetbrandnaddresslist: handsetbrandn
{
Public override void run ()
{
Console. writeline ("Run N-brand Mobile Phone Address Book ");
}
}
Call:
Handsetbrand AB;
AB = new handsetbrandmaddresslist ();
AB. Run ();
AB = new handsetbrandmgame ();
AB. Run ();
AB = new handsetbrandnaddresslist ();
AB. Run ();
AB = new handsetbrandngame ();
AB. Run ();
Console. Read ();
The second scheme of the dish is as follows:
The Code is as follows:
Public class Program
{
Static void main (string [] ARGs)
{
Handsetsoft AB;
AB = new handsetbrandmgame ();
AB. Run ();
AB = new handsetbrandngame ();
AB. Run ();
AB = new handsetbrandmaddresslist ();
AB. Run ();
AB = new handsetbrandnaddresslist ();
AB. Run ();
Console. Read ();
}
}
// Mobile phone software
Public class handsetsoft
{
Public Virtual void run ()
{
}
}
// Address book
Public class handsetaddresslist: handsetsoft
{
}
// Game
Public class handsetgame: handsetsoft
{
}
// Mobile phone brand M game
Public class handsetbrandmgame: handsetgame
{
Public override void run ()
{
Console. writeline ("Running M-branded mobile games ");
}
}
// Game with mobile phone brand N
Public class handsetbrandngame: handsetgame
{
Public override void run ()
{
Console. writeline ("Running N-brand Mobile Games ");
}
}
// Address Book of mobile phone brand m
Public class handsetbrandmaddresslist: handsetaddresslist
{
Public override void run ()
{
Console. writeline ("Run M-branded mobile phone Address Book ");
}
}
// Address Book of mobile phone brand N
Public class handsetbrandnaddresslist: handsetaddresslist
{
Public override void run ()
{
Console. writeline ("Run N-brand Mobile Phone Address Book ");
}
}
The following figure shows the bridge mode solution. The structure is shown first:
The Code is as follows:
Static void main (string [] ARGs)
{
Handsetbrand AB;
AB = new handsetbrandn ();
AB. sethandsetsoft (New handsetgame ());
AB. Run ();
AB. sethandsetsoft (New handsetaddresslist ());
AB. Run ();
AB = new handsetbrandm ();
AB. sethandsetsoft (New handsetgame ());
AB. Run ();
AB. sethandsetsoft (New handsetaddresslist ());
AB. Run ();
Console. Read ();
}
}
// Mobile phone brand
Public abstract class handsetbrand
{
Protected handsetsoft soft;
// Set the mobile phone software
Public void sethandsetsoft (handsetsoft soft)
{
This. Soft = soft;
}
// Run
Public abstract void run ();
}
// Mobile phone brand N
Public class handsetbrandn: handsetbrand
{
Public override void run ()
{
Soft. Run ();
}
}
// Mobile phone brand m
Public class handsetbrandm: handsetbrand
{
Public override void run ()
{
Soft. Run ();
}
}
// Mobile phone brand s
Public class handsetbrands: handsetbrand
{
Public override void run ()
{
Soft. Run ();
}
}
// Mobile phone software
Public abstract class handsetsoft
{
Public abstract void run ();
}
// Mobile games
Public class handsetgame: handsetsoft
{
Public override void run ()
{
Console. writeline ("running mobile games ");
}
}
// Mobile Address Book
Public class handsetaddresslist: handsetsoft
{
Public override void run ()
{
Console. writeline ("Run mobile phone Address Book ");
}
}
// MP3 playback on the mobile phone
Public class handsetmp3: handsetsoft
{
Public override void run ()
{
Console. writeline ("Running MP3 player on mobile phones ");
}
}
3. Summary
Results and implementation highlights:
1. The Bridge Mode decouples the inherent binding relationship between abstraction and implementation using the "Composite relationship between objects", so that abstraction and implementation can change along their respective dimensions.
2. The so-called abstraction and implementation are changed along their respective dimensions, that is, they are "subclass". After each subclass is obtained, they can be arbitrary and different models on different platforms can be obtained.
3. The bridge mode is sometimes similar to a multi-inheritance scheme, but a multi-inheritance scheme often violates the single responsibility principle of the class (that is, a class has only one reason for change), and the reusability is poor. The bridge mode is a better solution than multi-inheritance solutions.
4. the bridge mode is generally used in "two very strong changing dimensions". Sometimes, even if there are two changing dimensions, however, the changing dimension in a certain direction is not drastic-in other words, the two changes do not result in cross-border results and do not have to use the bridge mode.
When to use
The bridge mode should be used in the following situations:
1. If a system needs to increase more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.
2. The design requires that any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
3. A component has more than one abstract role and actual role, and the system needs dynamic coupling between them.
4. Although there is no problem in using inheritance in the system, abstract roles and specific roles need to be changed independently, and design requirements need to be managed independently.
References:
1. Big talk Design Model
2. http://terrylee.cnblogs.com/archive/2006/02/24/336652.html
Reprinted from: http://www.cnblogs.com/peida/archive/2008/07/17/1244690.html