Definition: Separate abstract parts from implementation parts so that they can all be changed independently.
Features: The bridge pattern is based on the minimum design principle of the class, and different classes bear different responsibilities by using encapsulation, aggregation and inheritance. Its main feature is to separate abstraction from behavior implementation, so that the independence of each part can be maintained and the functional expansion of them can be met.
Applications in enterprise-level development and commonly used frameworks: Multi-inheritance structure
It is just to understand the bridge pattern from the definition, and it is difficult to clarify its function. Here is an example to illustrate:
Computer City sells computers, and the types of computers are divided into tablets, laptops, and desktop computers; computer brands are divided into Lenovo, Dell, Asus, and China. If you build a class from the perspective of multiple inheritance, you need to first build a computer abstract class, and then tablets, laptops, and desktop computers inherit abstract computer classes respectively. Different computer brands must inherit these computer classification classes separately, and then implement the method. In this way, if you want to add a computer type, different computer brands must inherit the class separately, or add a computer brand, and then inherit different categories separately, which makes the creation complicated.
The idea of the bridge model is to implement it from different dimensions and reduce the development workload by increasing the coupling degree. For example, the above computer classification and computer brand classification belong to two dimensions. If we implement it from two dimensions separately, we can reduce the development complexity. This is the single principle of Java development.
The specific implementation code is as follows:
package com.test.bridge;interface Brand { public void sale();}class Dell implements Brand { public void sale() { System.out.println("Sales Dell brand computer"); }}class Lenovo implements Brand{ public void sale() { System.out.println("Sales Lenovo computer"); }}class Computer { protected Brand brand; public Computer(Brand b) { this.brand = b; } public void sale() { brand.sale(); }}class Desktop extends Computer{ public Desktop(Brand b) { super(b); } @Override public void sale() { super.sale(); System.out.println("Computer type is desktop computer"); }}class Laptop extends Computer{ public Laptop(Brand b) { super(b); } public void sale() { super.sale(); System.out.println("Computer type is laptop"); }}class Pad extends Computer{ public Pad(Brand b) { super(b); } @Override public void sale() { super.sale(); System.out.println("Computer type is tablet"); }}public class Demo { public static void main(String[] args) { //Sales Lenovo notebook Brand b = new Lenovo(); Computer c = new Laptop(b); c.sale(); //Sales dell desktop Brand b1 = new Dell(); Computer c1 = new Desktop(b1); c1.sale(); }}Through the above examples, we can see that we put the brand (Brand) in the form of objects into the computer classification class, which effectively reduces the complexity of the computer, and reduces the geometricality, and implements it in two directions, making our thinking clearer.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.