Java Bridge Mode (Bridge Mode)

Source: Internet
Author: User
Tags 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 class MediumCoffee extends Coffee {
    Public MediumCoffee () {setCoffeeImp ();}
    Public void pourCoffee (){
    CoffeeImp coffeeImp = this. getCoffeeImp ();
    // Repeat the number of times to indicate whether the cup is a medium cup or a large cup.
    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 will repeat the number of times to indicate whether the cup is a medium cup or a large cup.
    For (int I = 0; I <5; I ++ ){
    CoffeeImp. pourCoffeeImp ();
    }
    }
    } The above are the specific implementation of the Medium cup and the large cup respectively. The behavior CoffeeImp will be inherited below: // Add milk
    Public class MilkCoffeeImp extends CoffeeImp {
    MilkCoffeeImp (){}
    Public void pourCoffeeImp (){
    System. out. println ("added Delicious Milk ");
    }
    }

    // Without milk
    Public class FragrantCoffeeImp extends CoffeeImp {
    FragrantCoffeeImp (){}
    Public void pourCoffeeImp (){
    System. out. println ("nothing, fragrance ");
    }
    } The basic framework of the Bridge mode has been set up. Do not forget to define the dynamic combination. Now we can have at least four types of coffee:
    1. Medium cup Plus milk in cup without milk big cup Plus milk big cup without milk
      Let's take a look at how it works dynamically. before using it, let's make preparations and design a Singleton class 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: // take out the milk
      CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton (new MilkCoffeeImp ());

      // Milk in cup
      MediumCoffee mediumCoffee = new MediumCoffee ();
      MediumCoffee. pourCoffee ();

      // 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. Correct CoffeeImp creation 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 that operates on different databases is abstracted into a behavior interface DAO, as follows:
      • Business Object (similar to Coffee)
        Implement some abstract business operations, such as searching for all orders from 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, and OrderDAOSybase (similar to MilkCoffeeImp FragrantCoffeeImp)
        For specific database operations, such as insert into statements, OrderDAOOracle is Oracle OrderDAOSybase is Sybase Database.
        Database (Cloudscape, Oracle, or Sybase database via jdbc api)

        Bridging ModeOverview
        Separate the abstract part from its implementation part 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. For example, this may be because the implementation part of the program should be selected or switched during the runtime. 2. Class abstraction and its implementation should be expanded by generating subclass methods. In this case, the Bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately. 3. Modifications to an abstract implementation part should not affect the customer, that is, the customer's Code does not have to be re-compiled. 4. As shown in the first class diagram in Figure 1, there are many classes to be generated. Such a hierarchical structure indicates that an object must be divided into two parts. 5. You want to share the implementation among multiple objects (reference count may be used), but the customer is not required to know this.
        Participants
        1. Define Action defines the abstract class interface. Maintain a pointer to an Implementor object. 2. RefinedAbstraction expands the interface defined by the specified action. 3. Implementor defines the interface for implementing classes. This interface does not have to be exactly the same as the interface for implementing actions. In fact, these two interfaces can be completely different. Generally, the Implementor interface only provides basic operations, while the role Action defines high-level operations based on these basic operations. 4. ConcreteImplementor implements the Implementor interface and defines its specific implementation.
        Class Diagram ExampleInvalid action
        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();}
        Refinedmediaaction
        Public class Man extends Person {public Man () {setType ("Man");} public void dress () {Clothing clothing = getClothing (); clothing. personDressCloth (this );}}
        Public class Lady extends Person {public Lady () {setType ("");} 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 () + "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 trousers women wear vests women wear trousers


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.