Bridge Definition: The abstraction and behavior are separated, independent, but can be dynamically combined. Why use bridge modeTypically, 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 for the abstract section:
Public abstract class coffee{
Coffeeimp coffeeimp;
public void Setcoffeeimp () {
Thi S.coffeeimp = Coffeeimpsingleton.getthecoffeimp ();
}
Public Coffeeimp Getcoffeeimp () {return this. Coffeeimp;}
Public abstract void Pourcoffee ();
}
where Coffeeimp is a non-dairy behavior interface, see its code as follows:
Public abstract class coffeeimp{
public abstract void Pourcoffeeimp ();
Now we have two abstract classes, which we inherit from each other, implementing concrete class: Medium Cup
public class Mediumcoffee extends coffee{
Public Mediumcoffee () {setcoffeeimp ();}
public void Pourcoffee () {
Coffeeimp coffeeimp = This.getcoffeeimp ();
We repeat the number of times to explain whether the cup or a large cup, repeat 2 times is the Cup
for (int i = 0; i < 2; i++) {
Coffeeimp.pourcoffeeimp ();
}
}
}
Big Cup
public class Supersizecoffee extends coffee{
Public Supersizecoffee () {setcoffeeimp ();}
public void Pourcoffee () {
Coffeeimp coffeeimp = This.getcoffeeimp ();
We repeat the number of times to explain whether the cup or a large cup, repeat 5 times is a big cup
for (int i = 0; i < 5; i++) {
Coffeeimp.pourcoffeeimp ();
}
}
}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 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, 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 class coffeeimpsingleton{
private static Coffeeimp Coffeeimp;
Public Coffeeimpsingleton (Coffeeimp coffeeimpin)
{this.coffeeimp = Coffeeimpin;}
public static Coffeeimp Getthecoffeeimp () {
return coffeeimp;
}
}See how the Medium cup plus milk and the big 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 ();
Large cup with milk
Supersizecoffee Supersizecoffee = new Supersizecoffee ();
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 EJBThere 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)
Bridging Mode
Overview
Separate the abstract part from its implementation, so that they can all change independently.
Applicability
1. You do not want to have a fixed binding relationship between the abstraction and its implementation section. For example, this may be because the implementation part of the program run time should be selectable or switched. 2. The abstraction of a class and its implementation should be augmented by methods that generate subclasses. Bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately. 3. Modifications to an abstract implementation section should not have an impact on the customer, i.e. the customer's code does not have to be recompiled. 4. As shown in the first class diagram in the intent section, there are many classes to build. such a class hierarchy illustrates that you must break up an object into two parts. 5. You want to share the implementation across multiple objects (you might use reference counting), but at the same time ask the customer not to know that.
participants
1.Abstraction defines an interface for an abstract class. maintains a pointer to an Implementor type object. 2.RefinedAbstraction expands the interface defined by abstraction. 3.Implementor defines an interface to the implementation class that does not have to be exactly the same as the abstraction interface. in fact, the two interfaces can be completely different. generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations. The 4.ConcreteImplementor implements the Implementor interface and defines its specific implementation.
class Diagram
Example
Abstraction
public abstract class Person { private clothing clothing; Private String type; Public clothing getclothing () { return clothing; } public void setclothing () { this.clothing = clothingfactory.getclothing (); } public void SetType (String type) { this.type = type; } Public String GetType () { return this.type; } public abstract void Dress ();}
refinedabstraction
public class Mans extends person {public man () { setType ("men"); } public void dress () { Clothing clothing = getclothing (); Clothing.persondresscloth (this);} }
public class Lady extends man {public lady () { setType ("woman"); } public void dress () { Clothing clothing = getclothing (); Clothing.persondresscloth (this);} }
implementor
Public abstract class Clothing {public abstract void Persondresscloth (person person);
Concreteimplementor
public class Jacket extends clothing {public void Persondresscloth (person person) { System.out.println ( Person.gettype () + "Wear Vest");} }
public class Trouser extends clothing {public void Persondresscloth (person person) { System.out.println ( Person.gettype () + "wear Pants");} }
Test
public class Test {public static void Main (string[] args) {person man = new Man (); Person lady = new lady (); Clothing jacket = new Jacket (); Clothing trouser = new trouser (); Jacket.persondresscloth (man); Trouser.persondresscloth (man); Jacket.persondresscloth (lady); Trouser.persondresscloth (Lady); }}
result
Men wear vests, men wear pants, women wear vests, women wear pants.
Java Bridging mode (bridge mode)