Overview
In software systems, some types of software systems have two or more dimensional changes due to their own logic. How can we deal with such "multi-dimensional changes "? How can we use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity?
Example: Suppose we need at least four shape classes to draw a rectangle, a circle, an ellipse, or a square, but if the drawing needs to have different colors, such as red, green, and blue, there are at least two solutions:
• The first design scheme is to provide a set of color versions for each shape. • The second design scheme is to combine shapes and colors based on actual needs.
Solution 1:
Solution 2:
For systems with two changing dimensions (that is, the reasons for two changes), solution 2 is used to design systems with fewer classes, and system expansion is more convenient. Solution 2 is the application of the bridge mode. In the bridge mode, the inheritance relationship is converted into an association relationship, which reduces the coupling between classes and reduces the amount of code writing.
Concept
Separate the abstract part from the implementation part so that they can all change independently.
When an abstraction may have multiple implementations, inheritance is usually used to coordinate them. Abstract class definition for this abstract interface. The specific subclass is implemented in different ways, but this method is sometimes not flexible enough. The Inheritance Mechanism binds the abstract Part with its line of sight, making it difficult to modify, expand, and use the abstract Part and implementation part independently.
To understand the bridge mode, we need to understand how to decouple abstract action and implementation so that the two can change independently. • Abstraction: Abstraction ignores some information and treats different entities as the same entities. In object orientation, the process of extracting the common properties of objects to form classes is abstract. • Implementation: The specific implementation provided for abstraction is implementation. abstraction and implementation are a pair of reciprocal concepts. The objects produced by implementation are more specific than those produced by abstraction, is the product of further concrete abstraction. • Decoupling: decoupling is to free up coupling between abstraction and reality, or change strong associations between them into weak associations, change the inheritance relationship between two roles to the association relationship. In the bridge mode, decoupling refers to the Association (combination or aggregation) between the abstraction and implementation of a software system, rather than the inheritance relationship, so that the two can change relatively independently, this is the purpose of the bridge mode.
Applicable
- You do not want to have a fixed bonding relationship between the abstraction and its implementation. For example, the implementation part can be selected or switched during the running time of the program.
- Class abstractions and their views can be expanded by generating sub-classes. In this case, the bridge mode allows you to combine different abstract interfaces and implementation parts and expand them.
- Modifications to an abstract implementation should not affect the customer, that is, the customer's Code does not need to be re-compiled.
- You want to completely hide the abstract implementation part from the customer.
- You want to share the implementation among multiple implementations, but the customer is not required to know this.
Class Diagram
Role
Abstract action: defines the abstract class interface and maintains a pointer to an object of the implementor type.
Extension abstract class (refined1_action): expands the interface defined by the extension action
Implementation class interface (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.
Concreteimplementor: implements the implementor interface and defines its specific implementation.
Code
Abstractshape. Java
package com.yydcdut;public abstract class AbstractShape { Color color; public AbstractShape(Color color) { super(); this.color = color; } public abstract void draw();}
Color Interface
package com.yydcdut;public interface Color { String getColor();}
Square squre inherits the abstract graph class
Package COM. yydcdut; public class square extends abstractshape {Public Square (color) {super (color) ;}@ override public void draw () {system. out. println ("use" + color. getcolor () + "draw a square ");}}
Circular cirlce inherits abstract graphics classes
Package COM. yydcdut; public class circle extends actshape {public circle (color) {super (color) ;}@ override public void draw () {system. out. println ("use" + color. getcolor () + "circle ");}}
Red-red color Interface
Package com. yydcdut; public class red implements color {@ override Public String getcolor () {return "red ";}}
Green green color
Package com. yydcdut; public class green implements color {@ override Public String getcolor () {return "green ";}}
Test:
package com.yydcdut;public class Main { public static void main(String[] args) { Color color = new Green(); AbstractShape shape = new Square(color); shape.draw(); }}
Effect:
Draw a square in green
Summary
The bridge mode has the following advantages:
- An Implementation of the separation interface and its implementation part may not be bound to an interface without changing. The implementation of an abstract class can be configured at runtime, and an object can even be changed at runtime. Separating the compile action from implementor helps reduce the dependency on partial compilation time. When you change an implementation class, you do not need to recompile the compile action class and its client program. To ensure the binary compatibility between different versions of a class library, this feature must be applied. In addition, the separation of interfaces and implementations helps to Form layers to produce a better structured system. The High-level part of the system only needs to know the role action and implementor.
- To improve scalability, you can expand the hierarchical structure of the role action and implementor independently.
- Implementation Details are transparent to customers. You can hide implementation details to customers, such as sharing implementor objects and corresponding reference counting mechanisms (if any ).
Disadvantages of the Bridge Mode
- The introduction of the Bridge Mode increases the understanding and design difficulty of the system. Since the aggregation association is established on the abstraction layer, developers are required to design and program the abstraction.
- The bridge mode requires correct identification of two independent dimensions in the system, so the scope of use has certain limitations.
Simulated brush:
Now we need to provide 3 models of paint brushes, which can draw 5 different colors. If you use crayons, we need 3*5 = 15 crayons, that is to say, you must prepare 15 specific crayons. If you use a brush, you only need three types of brush, plus five pigment boxes. You can use 3 + 5 = 8 types to implement 15 crayons.
In fact, the key difference between a crayon and a brush is whether the pen and color can be separated. Decoupling abstract action and implementation so that the two can change independently ". The key lies in the decoupling. The color of a crayon is inseparable from that of a crayon. Therefore, 15 crayons of different colors and sizes must be used to draw a picture. The brush and face filter can be well decoupled, and their independent changes simplify the operation. Here, the concept of abstraction layer is: "paint with paint brush", while in implementation, there are five kinds of paint: big, medium, small, and medium, red, green, blue, black, and white, therefore, a combination of three or five types can appear. Each participant (brush and paint) can freely convert their own degrees of freedom.
Because the pen and color cannot be separated, the degree of freedom between the pen and the color cannot be changed separately, so that only 15 types of objects can be created to complete the task.
The Bridge Mode converts the inheritance relationship into a composite relationship, which reduces the coupling between systems and reduces the amount of code writing.
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
Java .zip
Reference: http://blog.csdn.net/hguisu/article/details/7529194