Bridge mode definition: The abstraction and behavior are separated, each independent, but can dynamically combine.
Anything objects have abstractions and behaviors, such as people, people are abstractions, people and women, etc .; people have behavior, behavior has a variety of specific performance, so "people" and "people's behavior" two The concept also reflects the abstraction and behavior.
In the basic concept of object-oriented design, the concept is actually consisting of two parts of the attribute and behavior. We can think that it is a stationary, an abstraction, in general, behavior is included in an object However, in some cases, we need to categorize these behaviors to form a total behavior interface, which is the use of bridge mode.
Why use? Do not want abstract parts and behavior there is a fixed binding relationship, but should be able to touch.
If an abstract or interface has multiple specific implementation (subclasses, concrete subclass), the relationship between these subclasses may have the following cases: 1. Concept of this multiple subclasses is parallel, as in front of example, piling There are two concrete classes: square piles and circular piles; the piles on both shapes are parallel, and there is no conceptual repetition. 2. The contents of this multiple subclasses overlap. Then we need to separate the abstract common partial and behaviors. It is ready to put it in an interface. Now you need to design two interfaces: abstract interface and behavior The interface is placed on abstraction and behavior. For example, a cup of coffee is an example, and the subclasses are achieved as four: medium cup plus milk, big cups and milk, medium cups do not add milk, big cups do not add milk.
However, we note that there is a concept overlap in the four subclasses above, and can be considered from another angle. These four classes are actually a combination of two roles: abstraction and behavior, where abstraction is: medium cup and big cup; behavior For: adding milk and can't add milk (such as add orange juice plus apple juice).
Implementing the four subclasses have a fixed binding relationship between abstraction and behavior. If the behavior of adding grape juice is increased in the future, it must add two classes: Cup plus grape juice and cup of grape juice. Obviously confusion, the spread is extremely poor.
Then we use the Bridge mode from the perspective of separation abstraction and behavior.
How to achieve? Coffee mentioned above as an example. We originally designed an 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 CoffeeImp getCoffeeImp () {return this.CoffeeImp;} public abstract void pourCoffee ();}
Among them, CoffeeImp is an active behavior interface that does not add milk, and the code is 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 a medium 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 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 ) {CoffeeImp.PourCoffeeImp ();}}} The above is the specific implementation of the Central 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, Summer");}}
Basic framework of Bridge mode We have already touched, don't forget to have a definition: dynamic combination, we can now drink at least four 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 EJB in EJB, which is separate from business logic and specific data resources because different databases have different database operations. Acting different databases will be independently abstract 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
DAOMPLEMENTOR is used in database operations.
2.Data Access Object (Similar to CoffeeImp)
Some abstraction 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 database.
4. Database (Cloudscape, Oracle, or Sybase Database Via JDBC API)