Design patterns, a must-have artifact for writing code...
What is bridge mode?
Bridge mode is to decouple abstraction and implementation, so that the two can be independent. This design mode is a structural design mode. What is the abstraction and implementation of decoupling, can be understood as abstract points out the features, functions it depends on how different needs, but the abstract function points (interfaces) have be bridged on to the original type, only With attention and realization. The original type of change, and abstract function point can be changed freely, the middle of the bridge has been built up .
In fact, the bridge mode does not only use the method of class inheritance, but focuses on the method of class aggregation to bridge, and aggregate (inject) abstract function points into the base class.
Benefits of Bridge Mode
What kind of problems is
The main point is to realize many kinds of functions, function points multiple dimensions, independent change , there is no association, it can be managed in accordance with the dimensions. For example, there are 2 dimensions, and each dimension has 3 implementations, but there is no relationship between different dimensions. If we do it according to the pairwise relationship between the dimensions, the number of implementation classes alone is already 2 * 3 = 6
classes. Not very suitable, but also coupled together.
Taking a computer as an example, it will be divided into different brands, such as Dell and Lenovo, as well as desktops and notebooks, so there will be many different categories and functions may be repeated. It is in view of this that we have to strip duplicate functions and use bridging methods to maintain abstracted common functional points.
If you add another brand, such as ASUS, then you have to add two more categories, which is obviously not suitable, and many functions of different categories may be repeated.
So how to deal with bridge mode? two different dimensions 161dcd69a8817d desktop and notebook , which is equivalent to maintaining it as a common attribute.
Code Demo demo
First, define an abstract computer class AbstractComputer
, in which there is an attribute ComputerType
, indicating the type of computer:
public abstract class AbstractComputer {
protected ComputerType type;
public void setType(ComputerType type) {
this.type = type;
}
public abstract void work();
}
Define three more types of computers: LenovoComputer
, AsusComputer
, DellComputer
:
public class LenovoComputer extends AbstractComputer{
@Override
public void work() {
System.out.print("联想");
type.feature();
}
}
public class AsusComputer extends AbstractComputer{
@Override
public void work() {
System.out.print("华硕");
type.feature();
}
}
public class DellComputer extends AbstractComputer{
@Override
public void work() {
System.out.print("戴尔");
type.feature();
}
}
The dimension of computer type also requires an abstract class ComputerType
, which has a method feature()
that describes the function:
public abstract class ComputerType {
// 功能特性
public abstract void feature();
}
In the dimension of computer type, we define two types: desktop and laptop:
public class DesktopComputerType extends ComputerType{
@Override
public void feature() {
System.out.println(" 台式机:性能强大,拓展性强");
}
}
public class LaptopComputerType extends ComputerType{
@Override
public void feature() {
System.out.println(" 笔记本电脑:小巧便携,办公不在话下");
}
}
Test it, when we need different collocations, we only need to set
one dimension 061dcd69a882c0 to the object, and then we can aggregate different brands and different types of computers:
public class BridgeTest {
public static void main(String[] args) {
ComputerType desktop = new DesktopComputerType();
LenovoComputer lenovoComputer = new LenovoComputer();
lenovoComputer.setType(desktop);
lenovoComputer.work();
ComputerType laptop = new LaptopComputerType();
DellComputer dellComputer = new DellComputer();
dellComputer.setType(laptop);
dellComputer.work();
}
}
Test Results:
联想 台式机:性能强大,拓展性强
戴尔 笔记本电脑:小巧便携,办公不在话下
in conclusion
Bridging mode, in essence, abstracts different dimensions or functions, and aggregates them into objects as attributes instead of inheritance. This reduces the number of classes to a certain extent, but if the changes between different dimensions are related, then various special judgments need to be made to use them, which is easy to cause confusion and should not be used. (Key: with composition / polymerization instead of inheritance relationship is achieved )
JDBC
, engaged Java
students should know that this is a Java
unified access to the database API
, can operate Mysql
, Oracle
, the main use of design patterns is bridge mode, are interested can find out Driver
driver class source code management .
【About the author】 :
Qin Huai, [ 161dcd69a883d0 Qin Huai Grocery Store ], the road of technology is not at one time, the mountains are high and the rivers are long, even if it is slow, it is endless. Personal Writing direction: the Java source code parsing,
JDBC
, Mybatis
, Spring
, redis
, distributed,
wins the Offer,
LeetCode
etc., carefully write each article, do not like the title of the party, do not like bells and whistles, mostly to write a series of articles , I cannot guarantee that what I have written is completely correct, but I guarantee that what I have written has been practiced or searched for information. For omissions or errors, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。