Bridge definition: The abstraction and behavior are separated, independent, but can be dynamically combined.
Why Use Bridge mode
Typically, when an abstract class or interface has more than one specific implementation (concrete subclass), the relationships between these concrete may have the following two types:
- These specific implementations happen to be parallel, as in the previous example, piling, there are two concrete class: square piles and round piles; the piles on these two shapes are tied and there is no conceptual repetition, so we can just use inheritance.
- In practical applications, it is often possible to have conceptual overlap between these multiple concrete classes. Then we need to separate the common parts of the abstract and the common part of the behavior, which is intended to be placed in an interface, and now we need to design two interfaces, respectively, to place abstractions and behaviors.
For example, a cup of coffee, for example, has a medium cup and a large cup of the points, but also with milk without milk. If the simple inheritance, the four specific implementation (medium cup with milk without milk) there is a concept overlap, because there is a medium cup with milk, there is also a medium cup without milk, if again in the Cup this layer to achieve two inheritance, is obviously chaotic, very poor extensibility. Then we use bridge mode to implement it.
How to Implement Bridge mode
Take the coffee mentioned above as an example. We originally intended to design only an interface (abstract class), after using bridge mode, we need to separate the abstraction and behavior, the addition of milk and non-dairy belong to the behavior, we will abstract them into a specialized behavior interface.
First look at the interface code of the abstract section:
Public Abstract class coffee{coffeeimp coffeeimp; Public void Setcoffeeimp () { this. Coffeeimp = coffeeimpsingleton.getthecoffeimp ();} Public Coffeeimp Getcoffeeimp () {returnthis. Coffeeimp;} Publicabstractvoid pourcoffee ();}
Where Coffeeimp is a non-dairy behavior interface, see its code as follows:
Public Abstract class coffeeimp{ publicabstractvoid pourcoffeeimp ();}
Now that we have two abstract classes, we inherit them separately to implement the concrete class:
// Middle Cup class mediumcoffee extends coffee{ public Mediumcoffee () {setcoffeeimp ();} public void Pourcoffee () {coffeeimp coffeeimp = this .getcoffeeimp (); // We repeat the number of times to indicate whether the cup or a large cup, repeat 2 times is the Middle Cup Span style= "color: #0000ff;" >for (int i = 0; i < 2; I++) {Coffeeimp.po Urcoffeeimp (); }}}
// Big Cup class supersizecoffee extends coffee{ public Supersizecoffee () {setcoffeeimp ();} public void Pourcoffee () {coffeeimp coffeeimp = this .getcoffeeimp (); // We repeat the number of times to indicate whether the cup or the Big Cup, repeat 5 times is the Big Cup Span style= "color: #0000ff;" >for (int i = 0; i < 5; I++) {Coffeeimp.po Urcoffeeimp (); }}}
The above is the specific implementation of the Cup and the Big Cup. The following is the inheritance of the behavior coffeeimp:
//Add Milk Public classMilkcoffeeimpextendscoffeeimp{Milkcoffeeimp () {} Public voidPourcoffeeimp () {System.out.println ("With the delicious milk."); }}//no milk . Public classFragrantcoffeeimpextendscoffeeimp{Fragrantcoffeeimp () {} Public voidPourcoffeeimp () {System.out.println ("Nothing to add, fragrance"); }}
The basic framework of bridge mode we've set it up, and don't forget that there is another sentence in the definition: dynamic combination, we can now drink at least four kinds of coffee:
- Medium Cup plus milk
- Medium Cup without milk
- Large cup with milk
- Big Cup without milk
To see how the dynamic combination, before using, we do a preparatory work, design a single-state class (Singleton) to hold the current coffeeimp:
Public classcoffeeimpsingleton{Private Staticcoffeeimp Coffeeimp; PublicCoffeeimpsingleton (Coffeeimp coffeeimpin) { This. Coffeeimp =Coffeeimpin;} Public Staticcoffeeimp Getthecoffeeimp () {returnCoffeeimp;}} See how the Medium cup plus milk and the big cup of milk come out://take out the milk .Coffeeimpsingleton Coffeeimpsingleton =NewCoffeeimpsingleton (Newmilkcoffeeimp ());//Medium cup plus milkMediumcoffee Mediumcoffee =NewMediumcoffee (); Mediumcoffee.pourcoffee ();//large cup with milkSupersizecoffee Supersizecoffee =NewSupersizecoffee (); Supersizecoffee.pourcoffee ();
Note: The execution classes of bridge mode, such as Coffeeimp and coffee, are one-to-one relationships, and the correct creation of COFFEEIMP is the key to the pattern.
Application of bridge mode in EJB
There is a data Access Object (DAO) pattern in the EJB, which separates the business logic from the specific data resources because different databases have different database operations. The behavior of manipulating different databases is independently abstracted into a behavior interface DAO, as follows:
- Business Object (similar to coffee)
Implement some abstract business operations: Looking for a user under all orders. The database operations involved use Daoimplementor.
- Data Access Object (similar to Coffeeimp)
Some abstract operations on database resources.
- Daoimplementor such as Orderdaocs, Orderdaooracle, Orderdaosybase (similar to Milkcoffeeimp fragrantcoffeeimp)
Specific database operations, such as the "INSERT into" statement, Orderdaooracle is the Oracle orderdaosybase is the Sybase database.
- Databases (Cloudscape, Oracle, or Sybase database via JDBC API)
Java Bridging mode (bridge mode)