Objective
This time introduces the second mode in structural design mode, bridging mode. The purpose of bridging mode is to understand the decoupling, the loose coupling is more advantageous to the extension, but will increase the corresponding code quantity and the design difficulty.
Bridging mode
bridging mode is a way to decouple abstraction from implementation, allowing the two to change independently. convenient for each part of the extension, as well as individual maintenance. There is a bridge between the abstract side and the realization side, so that the dependencies of the two can be established through this bridge.
Example
Three small animals to cross the river, respectively, is the pig, chicken, pony, pig to go to the open space across the river to bask in the sun, the chicken to go to the woods across the river to find insects to eat, pony to go across the lawn to eat grass. Then all three of them will pass through the bridge to cross the river. With the scenario below for the implementation of the code, first create a small bridge interface.
/***/Publicinterface Bridge {/** * Destination */ void Targetland ();}
Since the destination of three small animals is different, each destination corresponds to an implementation.
The destination of the pig
/***/Publicclassimplements bridge{/** * Destination * / @Override publicvoid Targetland () { System.out.println ("Empty place, basking in the Sun");} }
Chick's destination
/***/Publicclassimplements bridge{/** * Destination * / @Override publicvoid Targetland () { System.out.println ("grove, foraging. "); }}
Pony's Destination
/***/Publicclassimplements bridge{/** * Destination * / @Override publicvoid Targetland () { System.out.println ("The prairie, enjoy the Pentium. "); }}
The following is an abstraction, where each small animal has to cross the bridge to a different destination, so they all want the same cross-bridge behavior. So define an animal abstract class.
/***/publicabstractclass Animal {/** * Bridge ; /** * Bridge * * * abstractvoid Willtodo ();}
Pig
/***/Publicclassextends Animal {/** * Bridge */ @Override publicvoid Willtodo () { System.out.println ( "I'm a pig to cross the Bridge");} }
Chicken
/***/Publicclassextends Animal {/** * River bridge */ @Override publicvoid Willtodo () { System.out.println ( "I'm the chick going across the bridge");} }
Xiao Ma
/***/Publicclassextends Animal {/** * Bridge */ @Override publicvoid Willtodo () { System.out.println ( "I'm the pony going to the bridge");} }
Test examples
Public class Testbridge { publicstaticvoid main (string[] args) { new chick (); New Forest (); Animal.willtodo (); Animal.bridge.targetLand (); }}
Run results
I'm a chick going across the bridge to the woods and foraging.
This is an example of a complete bridging pattern, which decouples the small animal from the destination to go. If you come up with a small animal, such as a duckling, it only needs to inherit the animal class, and if its destination already exists, use the existing destination class directly, if the destination you want to go to does not exist (for example, the duckling is going to the pond), then you can create a destination for the pond, Then it can be realized from bridge.
Structure
Here's a look at the structure of the bridging pattern, as shown in.
From the above structure, we can see that the bridging mode is actually divided into four characters.
abstract Role (Animal Class): Define abstractions and save a reference to the implemented object.
Abstract Extended Roles (Chick, piglet, pony, etc.): the ability to implement and extend abstract roles.
implementation role (Bridge Interface): This role gives an interface to the implementation role and defines the behavior of the implementing role.
specific implementation of the role (Vacantland, grassland, forest and other destination classes): Implement the implementation of the role interface of the specific class.
Advantages and disadvantages of bridging mode
1. Separation of abstraction and implementation parts:
The abstraction and implementation parts are separated, which improves the flexibility of the system, which helps to layer the system so as to produce a better structured system.
2. Better extensibility:
Because the abstract part and the implementation part are separated, so the two parts can be independently extended, each other, greatly improving the scalability of the system.
3, can switch dynamically:
Because of the separation of abstraction and implementation, when implementing bridging, you can implement dynamic selection and use of specific implementations, that is, during runtime dynamic switching implementation.
4, the number of sub-categories reduced:
In terms of abstraction and implementation of two dimensions, the number of subclasses of these two dimensions, if not bridged, is the product of the two dimension subclasses that are affected by the change. The number that is affected by the use of bridging mode is the subclass of two dimensions.
Disadvantages
It is not so easy to increase the comprehension and design difficulty of the system, because the aggregation relationship is defined in the abstraction layer, so the developer needs to design and program the abstraction.
To learn more about design patterns, see the Java Design Patterns Learning record-gof design pattern Overview.
Java Design pattern Learning record-bridging mode