Bridge definition: The separation of abstraction and behavior, independent, but dynamic combination.
Why Use Bridge mode
Typically, when an abstract class or interface has more than one specific implementation (concrete subclass), these concrete relationships may have the following two kinds:
1. There is a parallel between these many concrete implementations, as in the preceding example, piling, there are two concrete class: square and round piles; the piles on these two shapes are tied and there is no conceptual repetition, so we just use inheritance.
2. In practical applications, it is often possible to have conceptual overlap between these multiple concrete classes. So we need to separate the common parts of the abstract and the behavior, and we are ready to put them in an interface, and now we need to design two interfaces, and place abstractions and behaviors, respectively.
For example, a cup of coffee, for example, with a medium cup and a large cup, as well as the addition of milk without milk points. If with simple inheritance, these four concrete implementations (medium cup with milk without milk) there is a concept overlap, because there is a medium cup plus milk, also have medium cup without milk, if again in the middle of this layer and then realize two inheritance, it is obviously chaotic, very poor extensibility. Then we use the bridge model to implement it.
How to Implement Bridge mode
Take the coffee mentioned above as an example. We originally intended to design only one interface (abstract class), using bridge mode, we need to separate abstraction and behavior, add milk and no milk is the behavior, we abstract them into a special behavior interface.
Look at the interface code for the abstract section first:
Copy Code code as follows:
Public abstract class coffee{
Coffeeimp Coffeeimp;
public void Setcoffeeimp () {
This. Coffeeimp = Coffeeimpsingleton.getthecoffeimp ();
}
Public Coffeeimp Getcoffeeimp () {return this. Coffeeimp;}
public abstract void Pourcoffee ();
}
The Coffeeimp is a behavioral interface that adds no milk and looks at its code as follows:
Copy Code code as follows:
Public abstract class coffeeimp{
public abstract void Pourcoffeeimp ();
}
Now that we have two abstract classes, we inherit them separately, implementing concrete class:
Copy Code code as follows:
Medium Cup
public class Mediumcoffee extends coffee{
Public Mediumcoffee () {setcoffeeimp ();}
public void Pourcoffee () {
Coffeeimp coffeeimp = This.getcoffeeimp ();
We repeat the number of times to indicate whether the punch or the Big Cup, repeat 2 times is the medium cup
for (int i = 0; i < 2; i++) {
Coffeeimp.pourcoffeeimp ();
}
}
}
Copy Code code as follows:
Big Cup
public class Supersizecoffee extends coffee{
Public Supersizecoffee () {setcoffeeimp ();}
public void Pourcoffee () {
Coffeeimp coffeeimp = This.getcoffeeimp ();
We repeat the number of times to indicate whether the punch or the Big Cup, repeat 5 times is a big cup
for (int i = 0; i < 5; i++) {
Coffeeimp.pourcoffeeimp ();
}
}
}
The above is the concrete realization of the Medium cup and the Big Cup respectively. The following behavior Coffeeimp are inherited:
Copy Code code as follows:
Add milk
public class Milkcoffeeimp extends coffeeimp{
Milkcoffeeimp () {}
public void Pourcoffeeimp () {
System.out.println ("Add delicious milk");
}
}
No milk.
public class Fragrantcoffeeimp extends coffeeimp{
Fragrantcoffeeimp () {}
public void Pourcoffeeimp () {
System.out.println ("Nothing added, fragrance");
}
}
The basic framework of bridge mode we've set it up, don't forget the definition also has one sentence: dynamic combination, we can now drink at least four kinds of coffee:
1. Medium Cup plus milk
2. Medium Cup without milk
3. Big Cup plus milk
4. Large Cup without milk
See how it is dynamically combined, before using, we do a preparation and design a single State Class (Singleton) to hold the current coffeeimp:
Copy Code code as follows:
public class coffeeimpsingleton{
private static Coffeeimp Coffeeimp;
Public Coffeeimpsingleton (Coffeeimp coffeeimpin)
{this.coffeeimp = Coffeeimpin;}
public static Coffeeimp Getthecoffeeimp () {
return coffeeimp;
}
}
See how the Medium cup with milk and a large cup of milk come out:
Take out the milk
Coffeeimpsingleton Coffeeimpsingleton = new Coffeeimpsingleton (new Milkcoffeeimp ());
Medium Cup plus milk
Mediumcoffee Mediumcoffee = new Mediumcoffee ();
Mediumcoffee.pourcoffee ();
A large cup of milk
Supersizecoffee Supersizecoffee = new Supersizecoffee ();
Supersizecoffee.pourcoffee ();
Note: The execution classes in bridge mode, such as Coffeeimp and coffee, are one-to-one relationships, and the proper creation of COFFEEIMP is the key to this pattern.
The application of bridge mode in EJB
There is a data Access Object (DAO) pattern in the EJB that separates business logic from specific data resources because different databases have different database operations. The act of manipulating different databases is abstracted independently into a behavioral interface DAO, as follows:
1.Business Object (similar to coffee)
implements some abstract business operations, such as finding all orders under a user. Daoimplementor is used for database operations.
2.Data Access Object (similar to coffeeimp)
Some abstract manipulation of database resources.
&NBSP
3.DAOImplementor such as Orderdaocs, Orderdaooracle, Orderdaosybase (similar to Milkcoffeeimp fragrantcoffeeimp)
Specific database operations, such as "INSERT into" statements, Orderdaooracle is Oracle Orderdaosybase is a Sybase database.
4. Databases (Cloudscape, Oracle, or Sybase database via JDBC API)