Bridge Mode. intension is separated from implementor. Here, ITU is defined by common interfaces, and the implementation is factory, and the specific intent implemention1 is returned.
Public Interface Intention
{
VoidEcho (StringMessage );
}
Public Class Implemention1: Intention
{
Public Void Echo ( String Message)
{
Console. writeline ("Implemention1");
}
}
Public Class Factory
{
Public Static Intention instantiate ()
{
Return NewImplemention1 ();
}
}
Intent implemention1 can be easily replaced with another intent implemention2.
In this way, on the client side, I can write as follows:
// Intension OBJ = new implemention1 ();
Intention OBJ = Factory. instantiate ();
OBJ. Echo ();
The commented rows are not intended to be used. The work of specifying a specific object should be handed over to the factory. This factory can be extended to simplefactory, allowing the user to decide what kind of object to be generated.
We can also use generic interfaces to make the bridge mode more suitable for general scenarios, such:
Public Interface Intention < T >
{
VoidEcho (T message );
}
The above is the most basic bridge model, but in reality we often use a variant of the Bridge Model, which is mentioned in various blogs/forums/textbooks, the same is true even for the UML of gof,
The separation of intension and implementor is too much. In reality, the root cause is unclear about intent and implementation, which is often determined by ourselves. Therefore, we can change the definition:
If the system can change along multiple dimensions, you can separate and separate dimensions to achieve maximum decoupling.
-- The essence is: since it can be abstracted as multiplication, no addition should be used.
For example, if a watercolor system has 7 kinds of pigments and a large brush and a small brush, I want to design 14 types, they are the 7 colors of the big brush and the 7 colors of the small brush-there is no problem. If you add a brush set, you need to add seven colors of the brush set. If you add a paint set, then we need to add one color class for the big brush and seven color classes for the small brush. This is problematic. It violates the single responsibility principle of a class, that is, a class has only one reason for its change.
We call the pen model and pigment type the two dimensions of the watercolor system. The system can change according to these two dimensions. If the number of classes changes at the same time, the number of classes will surge. At this time, we need to abstract the model of the brush and the pigment types. In this case, there are only seven color classes and two brush models. Of course, there are two abstract base classes.
What is the difference between bridge and Creation Mode:
When only one part of a system is unstable, use the creation mode. When two or more parts are unstable, use bridge to decouple them and use factory to implement them respectively.