Design Mode Bridge-----2

Source: Internet
Author: User
Tags sybase sybase database

Bridge Mode definition :
The abstraction and behavior are separated, independent, but dynamically combined.

Any object has the abstract and the behavior of the points, such as human, human is a kind of abstract, men and women and so on, people have behavior, behavior also have all kinds of concrete performance, so, "man" and "Human Behavior" two concepts also reflect the abstract and behavior of the points.

In the basic concept of object-oriented design, the concept of an object is actually composed of two parts of the attribute and the behavior, which we can consider to be static, an abstraction, in general, the behavior is contained in an object, but, in some cases, we need to classify these behaviors, Form a general behavior interface, this is the use of bridge mode.

Why use?
It is not desirable to have a fixed binding relationship between the abstract part and the behavior, but should be dynamically connected.

If an abstract class or interface has multiple implementations (subclasses, concrete subclass), the relationships between these subclasses may have the following two cases:
1. The concept of these subcategories is tied, as in the preceding example, piling, with two concrete class: square and round piles; the piles on these two shapes are tied and there is no conceptual repetition.

2. There is a conceptual overlap in the contents of these subcategories. Then we need to separate the common parts of the abstract from the part of the Act, which is intended to be placed in an interface, and now we need to design two interfaces: An abstract interface and a behavioral interface, where abstraction and behavior are placed respectively.

For example, a cup of coffee as an example, the subclass implementation class is four: Medium cup with milk, large cup plus milk, medium cup without milk, large cup without milk.

However, we note that there are conceptual overlaps in the four subclasses above, from another perspective, these four classes are actually a combination of two roles: abstraction and behavior, which are abstracted as: medium and large; behavior: Add milk without milk (e.g. orange juice Plus apple juice).

Implementing four subclasses has a fixed binding relationship between abstraction and behavior, and if you add the grape juice dynamically later, you must add another two classes: Medium cup with grape juice and large cup with grape juice. Obviously chaotic and extremely poor scalability.

So we're going to do this from the perspective of separation of abstraction and behavior, using bridge mode.

How to achieve it?
Take the coffee mentioned above as an example. We originally intended to design only one interface (abstract class), using bridge mode, we need to separate abstraction and behavior, add milk and no milk is the behavior, we abstract them into a special behavior interface.

Look at the interface code for the abstract section first:

Public abstract class Coffee
{
Coffeeimp Coffeeimp;

public void Setcoffeeimp () {
This. Coffeeimp = Coffeeimpsingleton.getthecoffeimp ();
}

Public Coffeeimp Getcoffeeimp () {return this. Coffeeimp;}

public abstract void Pourcoffee ();
}

The Coffeeimp is a behavioral interface that adds no milk and looks at its code as follows:

Public abstract class Coffeeimp
{
public abstract void Pourcoffeeimp ();
}

Now that we have two abstract classes, we inherit them separately, implementing concrete class:

Medium Cup
public class Mediumcoffee extends Coffee
{
Public Mediumcoffee () {setcoffeeimp ();}

public void Pourcoffee ()
{
Coffeeimp coffeeimp = This.getcoffeeimp ();
We repeat the number of times to indicate whether the punch or the Big Cup, repeat 2 times is the 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 repeat the number of times to indicate whether the punch or the Big Cup, repeat 5 times is a big cup
for (int i = 0; i < 5; i++)
{

Coffeeimp.pourcoffeeimp ();
}
  
}
}

The above is the concrete realization of the Medium cup and the Big Cup respectively. The following behavior Coffeeimp are inherited:

Add milk
public class Milkcoffeeimp extends Coffeeimp
{
Milkcoffeeimp () {}

public void Pourcoffeeimp ()
{
System.out.println ("Add delicious milk");
}
}

No milk.
public class Fragrantcoffeeimp extends Coffeeimp
{
Fragrantcoffeeimp () {}

public void Pourcoffeeimp ()
{
System.out.println ("Nothing added, fragrance");
}
}

The basic framework of bridge mode we've set it up, don't forget the definition also has one sentence: dynamic combination, we can now drink at least four kinds of coffee:
1. Medium Cup plus milk
2. Medium Cup without milk
3. Big Cup plus milk
4. Large Cup without milk

See how it is dynamically combined, before using, we do a preparation and design a single State Class (Singleton) 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;
}
}

See how the Medium cup with milk and a large cup of milk come out:

Take out the milk
Coffeeimpsingleton Coffeeimpsingleton = new Coffeeimpsingleton (new Milkcoffeeimp ());

Medium Cup plus milk
Mediumcoffee Mediumcoffee = new Mediumcoffee ();
Mediumcoffee.pourcoffee ();

A 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, and the proper creation of COFFEEIMP is the key to this pattern,

The application of bridge mode in EJB
There is a data Access Object (DAO) pattern in the EJB that separates business logic from specific data resources because different databases have different database operations. The behavior of manipulating different databases is abstracted into a behavioral interface DAO. as follows:

1.Business Object (similar to coffee)

Implement some abstract business operations: Find all orders under a user

Daoimplementor is used for database operations.

2.Data Access Object (similar to Coffeeimp)

Some abstract manipulation of database resources

3.DAOImplementor such as Orderdaocs, Orderdaooracle, Orderdaosybase (similar to Milkcoffeeimp fragrantcoffeeimp)

Specific database operations, such as "INSERT into" statements, Orderdaooracle is Oracle Orderdaosybase is a Sybase database.

4. Databases (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.