Understanding of the bridge model

Source: Internet
Author: User

Right Bridge the understanding of the pattern

Absrtact: This paper first explains the definition of bridge mode. Then through an example, the bridge pattern is implemented step by step.

Before I start, let me give you three classic quotes: design to interfaces. Find what varies and encapsulate it. Favor composition over inheritance. Back we will repeat, and is used repeatedly. I think in the design of these three words should be firmly imprinted in the brain.

A Defined

According to the definition of GOF, the purpose of the bridge model is to "decouple the abstraction from its implementation so that the two can be changed independently." "Abstract" and "its implementation", the most misunderstood in this definition. Because these two words have corresponding keywords in the object-oriented language. In Java it is "abstract" and "implement", so it is easy to confuse the thought of decoupling an abstract class and its implementation class. In fact, the implementation here refers to an abstract class and its derived class to implement its own object, further saying that the abstraction here refers to a concept or an object in an inheritance system, and the implementation is abstracted Use and complete your own functions. For example, this is an example of Brother Lu Zhenyu (http://www.cnblogs.com/zhenyulu/articles/67016.html), this is the most classic example I have ever seen, so that I have to repeat it here:

When we were kids we used to draw crayons, a box of crayons 12 colors. At first I was using the smallest crayon to draw a sun father-in-law, moon mother-in-law enough. Then began to draw some abstract works, you have to change the medium, or draw a background to be traced half-day, a box of medium is also 12 colors. Then I began to turn to the uninhibited pie, the medium is somewhat stretched, had to change the size of a large box of only 12 kinds of colors. You see, a less famous painter like me needs 36 kinds of brushes, wow, it's too much trouble. But as far as I can see, other painters who are more famous than me do not have so many pens, they only have a few brushes and some pigments, which solves the problem of "kind of explosion" of crayons. As shown in the following picture, note that the picture is also stolen from the LU Brother's website.


I'm going to use 36 kinds of crayons.

Mr. Qi Baishi only used 3 kinds of brushes and 12 kinds of pigments

Back to the definition above, the definition of the abstract refers to the brush, the realization is the pigment. Pigments are used by the brush to complete the function of the brush.


(http://www.niufish.com/books/Pattern/com/niufish/pattern/bridge/package-use.html)


The following points are considered to be flexible in the above figure:

1. Abstraction and implementor of the aggregation. The relationship can be replaced by the refinedabstraction and implementor aggregation relationships. The reason is that Java programming time abstraction may be implemented as interface, can not form an aggregation relationship with implementor.

2. Operation method is not necessarily done in the abstraction, the reason is the same, but must be declared.

Visible in the bridge model has two inheritance system, in order to facilitate the description we call the left of the abstraction inheritance system, the right for the Implementor inheritance system. Abstraction uses Implementor to complete its functions. At the same time, the model allows abstraction and implementor to change independently (the so-called change, which I think is derivation).

Two Solve the problem

It has been said that the concept of writing a brush is to solve the "kind of explosion" problem of crayons. 3x12 has become a 3+12, and in the future the brush type and pigment species can be independently expanded. The above example is good for showing the characteristics and advantages of this design pattern, but after all, we are rarely so fortunate to have such a simple problem in our actual coding. Below I use a relatively complex question to redefine the pattern, and the code for the story of crayons and brushes can be found on the links given above.

Three A more complex example

Here we give a more complex example, and describe it in a step-by-step manner, adding new features and constraints gradually. Imagine we're going to do an editor (Editor), you can open a text file, but different files require a different editor to open. For example, ". txt" files are opened with a text editor, and the ". Xml" file is opened with an XML editor.


The code is as follows:

Editor interface

package Com.gemplus.editor;

Public Abstract Interface Editor {

Public void OpenFile (String path);

}

TextEditor implementation

package Com.gemplus.editor;
 Public class Implements Editor {
 Public void OpenFile (String path) {
System. Out . println ("Open file with Text Editor.") FileName: " + path");
}
}

XmlEditor implementation

package Com.gemplus.editor;
 Public class Implements Editor {
 Public void OpenFile (String path) {
System. Out . println ("Open file with XML Editor.") FileName: " + path");
}
}

Client

package Com.gemplus.editor;
 Public class Client {
 Public Static void Main (string[] args) {
New XmlEditor ();

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.