Bridge definition: The abstraction and behavior are separated, each independent, but can dynamically bind.
Why use? Usually, when there is a number of concrete subclass, these Concrete is related to the following: 1. This more specific implementations are just in parallel, as in the previous example, Piling, there are two concrete class: square piles and round piles; these two piles are parallel, there is no conceptual repetition, then we can use inheritance. 2. Practical applications, often possible There is a conceptual overlap between many Concrete Class. Then we need to open the abstract common partial and behavior. It is ready to put it in a interface. Now you need to design two interfaces, laying abstraction and behavior separately For example, a cup of coffee has been used as an example, with a mid-cup and a cup of cups, and there is also a score of adding milk. If you use a simple inheritance, these four specific implementation (CC Cup add milk does not add milk) There is a concept overlap, because there is a cup of cups and milk, and there is a medium cup does not add milk. If it is in the middle cup, it will achieve two inheritance, which is clearly confusing, the extension is very poor. Then we use the Bridge mode to implement it. .
How to achieve? Coffee mentioned above as an example. We originally designed a interface (abstract class), after using Bridge mode, we need to separate abstracts and behaviors, plus, and do not add milk, we will abstract them A special behavior interface.
First look at the interface code of the abstract part:
public abstract class Coffee {CoffeeImp coffeeImp; public void setCoffeeImp () {this.CoffeeImp = CoffeeImpSingleton.getTheCoffeImp ();} public SodaImp getCoffeeImp () {return this.CoffeeImp;} public abstract void pourCoffee ();}
Among them, CoffeeImp is an incomplete behavior interface to see its code as follows:
Public Abstract Class CoffeeImp {Public Abstract Void PourCoffeeImp ();
Now we have two abstract classes, and we will inherit them, implement the Concrete Class:
// cup public class MediumCoffee extends Coffee {public MediumCoffee () {setCoffeeImp ();} public void pourCoffee () {CoffeeImp coffeeImp = this.getCoffeeImp (); // we described the number of repetitions is red cup or mug , Repeat 2 times is the medium cup for (int i = 0; i <2; i ) {sodaimp.PourCoffeeImp ();}}} // big cup public class supersizecoffee extends coffee {public supersizecoffee () {setCoffeeImp ();} Public void PourCoffee () {coffeeImp coffeeimp = this.getCoffeeImp (); // We use the number of repetitions to be a cup or a big cup, repeat 5 times is a big cup for (INT i = 0; i <5; i ) {Sodaimp.pourcoffeeImp ();}}}
The above is the specific implementation of the middle cup and the cup. The following will inherit the behavior CoffeeImp:
// milk public class MilkCoffeeImp extends CoffeeImp {MilkCoffeeImp () {} public void pourCoffeeImp () {System.out.println ( "delicious milk plus");}} // without milk public class FragrantCoffeeImp extends CoffeeImp {FragrantCoffeeImp () {} Public void PourCoffeeImp () {system.out.println ("Nothing, Sceon");}} The basic framework of Bridge mode We have already touched, don't forget that there is still a sentence in definition: dynamic combination, We can now drink at least four kinds of coffee: 1. Central Cup plus milk 2. Central cup does not add milk 3. Cup plus milk 4. Cup does not add milk
See how you are dynamically combined, before use, we are prepared, design a single-state class (Singleton) used to Hold CoffeeImp:
public class CoffeeImpSingleton {private static CoffeeImp coffeeImp; public CoffeeImpSingleton (CoffeeImp coffeeImpIn) {this.coffeeImp = coffeeImpIn;} public static CoffeeImp getTheCoffeeImp () {return coffeeImp;}}
Take a look at how Cup plus milk and cup adds:
// out milk CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton (new MilkCoffeeImp ()); // cup milk MediumCoffee mediumCoffee = new MediumCoffee (); mediumCoffee.pourCoffee (); // large glass of milk SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee () SuperSizeCoffee.PourCoffee ();
Note: The execution class of Bridge mode such as CoffeeImp and Coffee are one-on-one relationship, and correctly create CoffeeImp is the key to this mode.
Bridge mode There is a Data Access Object (DAO) mode in the application EJB in EJB, which is separate of commercial logic and specific data resources, because different databases have different database operations. Operation of different libraries Independent abstraction into a behavior interface DAO. As follows:
1.Business Object (Similar Coffee)
Implement some abstract business operations: If you look for all orders under one user
DAOIMPLEMENTOR is used in the database operation.
2.Data Access Object (Similar to CoffeeImp)
Some abstract abstracts of database resource operations
3. DaoImplement, such as OrderDaocs, OrderDaooracle, ORDERDAOSYBASE (similar MilkCoffeeImp FragrantCoffeeImp)
For specific database operations, such as "Insert Into", ORDERDAOORACLE is Oracle ORDERDAOSYBASE is a Sybase repository.
4. Database (Cloudscape, Oracle, Or Sybase Database Via JDBC API)