Java Bridge Mode (Bridge Mode)

Source: Internet
Author: User
Tags sybase sybase database

Java Bridge Mode (Bridge Mode)
Bridge definition: separates abstraction and behavior, and is independent of each other, but can be dynamically combined. Why is Bridge Mode usually used? When an abstract class or interface has multiple concrete implementations (concrete subclass), there may be two relationships between these concrete: the multiple implementations are exactly tied. For example, the piling has two concrete classes: A Square Pile and a circular pile. The piles in these two shapes are tied in parallel, there is no conceptual repetition, so we only need to use inheritance. In practice, it is often possible that multiple concrete classes overlap conceptually. Therefore, we need to separate the abstract common part and the behavior common part. It was originally intended to be placed in an interface. Now we need to design two interfaces to place the abstract and behavior respectively. For example, a cup of coffee can be divided into a medium cup and a large cup, and there can be a plus or no milk. If we simply inherit from each other, there is a concept overlap between the four concrete implementations (medium cup and large cup without milk), because there are medium cup Plus milk, there are also middle cup without milk, if we implement two inheritance operations at the level of the cup, it is obviously confusing and the scalability is very poor. Then we use the Bridge mode to implement it. How to Implement the coffee mentioned above in the bridge mode is used as an example. We originally intended to design only one interface (abstract class). After using the Bridge mode, we need to separate abstraction and behavior. Adding milk and not adding milk are behaviors, we abstract them into a specialized behavior interface. First look at the abstract Part of the interface code: public abstract class Coffee {CoffeeImp coffeeImp; public void setCoffeeImp () {this. coffeeImp = CoffeeImpSingleton. getTheCoffeImp ();} public CoffeeImp getCoffeeImp () {return this. coffeeImp;} public abstract void pourCoffee (); CoffeeImp is an action interface without milk. The Code is as follows: public abstract class CoffeeImp {public abstract void pourCoffeeImp ();} now we have two abstract classes. Next we inherit them separately to implement concrete class: // medium cup public cl Ass MediumCoffee extends Coffee {public MediumCoffee () {setCoffeeImp ();} public void pourCoffee () {CoffeeImp coffeeImp = this. getCoffeeImp (); // The number of repetitions indicates whether the cup is a medium cup or a large cup. If the number of repetitions is two, the middle cup is for (int I = 0; I <2; I ++) {coffeeImp. pourCoffeeImp () ;}}// large cup public class SuperSizeCoffee extends Coffee {public SuperSizeCoffee () {setCoffeeImp ();} public void pourCoffee () {CoffeeImp coffeeImp = this. getCoffeeImp (); // we use The number of repetitions indicates whether it is a medium cup or a large cup. If it is repeated for five times, it is a large cup for (int I = 0; I <5; I ++) {coffeeImp. pourCoffeeImp () ;}}above are the specific implementation of the Medium cup and the large cup respectively. next we will inherit the CoffeeImp behavior: // Add the public class MilkCoffeeImp extends CoffeeImp {MilkCoffeeImp () {} public void pourCoffeeImp () {System. out. println ("Delicious Milk added");} // public class FragrantCoffeeImp extends CoffeeImp {FragrantCoffeeImp () {} public void pourCoffeeImp () {System. out. println ("nothing, fragrance");} Bri The basic framework of the dge mode has been set up. Do not forget to define the dynamic combination. Now we can have at least four types of coffee: in the cup of milk in the cup without milk big cup with milk big cup without milk to see how dynamic combination, before use, we make a preparation work, design a single state class (Singleton) used 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;} Let's see how the cup of milk and the big cup of milk come out: // get the CoffeeImpSingleton coffeeIm PSingleton = new CoffeeImpSingleton (new MilkCoffeeImp (); // cup with milk MediumCoffee mediumCoffee = new MediumCoffee (); mediumCoffee. pourCoffee (); // SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee (); superSizeCoffee. pourCoffee (); note: the execution classes such as CoffeeImp and Coffee in Bridge mode are one-to-one relationships. Creating CoffeeImp correctly is the key to this mode. The Bridge mode has a Data Access Object (DAO) mode in the application EJB of EJB. This separates the commercial logic from the specific Data resources, because different databases have different database operations. The behavior of operating different databases is abstracted into a behavior interface DAO, as follows: Business Object (similar to Coffee) implements some abstract Business operations, such as searching for all orders of a user. DAOImplementor is used for all database operations. Data Access Object (similar to CoffeeImp) Abstract operations on database resources. DAOImplementor such as OrderDAOCS, OrderDAOOracle, OrderDAOSybase (similar to MilkCoffeeImp FragrantCoffeeImp) specific database operations, such as "insert into" statements, OrderDAOOracle is Oracle OrderDAOSybase is Sybase Database. Database (Cloudscape, Oracle, or Sybase database via jdbc api)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.