Bridge of design patterns

Source: Internet
Author: User
Tags sybase database

Bridge definition:
Abstract and behavior are separated and independent, but can be dynamically combined.

Why?
Generally, when an abstract class or interface has multiple concrete implementations (concrete subclass), the relationships between these concrete may be as follows:
1. 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.

2. 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 it?
Take the coffee mentioned above 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 special 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 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 is 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. cup of milk
2. Cup without milk
3. Big cup Plus milk
4. 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 take a look at how a cup of milk and a big cup of milk come out:

// Take out 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,

Application of Bridge Mode in EJB
In EJB, there is a Data Access Object (DAO) mode, which separates business logic from 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:

1. Business Object (similar to Coffee)

Implement some abstract business operations: such as searching for all orders of a user

DAOImplementbor is used for all database operations.

2. Data Access Object (similar to CoffeeImp)

Some abstract operations on database resources www.2cto.com
3. 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 a Sybase database.

4. database (Cloudscape, Oracle, or Sybase database via jdbc api)


Author: tbwshc

Related Article

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.