Bridge pattern design pattern
foreword
1. Bridge mode (Bridge) refers to placing the implementation and abstraction in two different class levels, so that the two levels can be changed independently
2, is a structural design pattern
3. The Brideg pattern is based on the minimal design principle of classes, and allows different classes to assume different responsibilities through the use of encapsulation, aggregation, and inheritance. Sex and the expansion of functions to deal with them
Tip: The following is the main text of this article, the following cases are for reference
1. Core Concepts
1. The bridge mode replaces the multi-layer inheritance scheme, which can reduce the number of subclasses
2. Correctly identify two independently changing dimensions in the system (abstract, and implementation)
2. Use steps
interface
public interface Brand { void open(); void close(); void call(); }
Implementation class 1
public class Vivo implements Brand{ @Override public void open() { System.out.println("Vivo phone on"); } @Override public void close() { System.out.println("Vivo Phone off"); } @Override public void call() { System.out.println("Vivo phone call"); } }
Implementation class 2
public class XiaoMi implements Brand{ @Override public void open() { System.out.println("Xiaomi phone power on"); } @Override public void close() { System.out.println("Xiaomi mobile phone shuts down"); } @Override public void call() { System.out.println("xiaomi phone call"); } }
abstract class (bridge)
public abstract class Phone { private Brand brand; public Phone(Brand brand) { this.brand = brand; } protected void open(){ brand.open(); } protected void close(){ brand.close(); } protected void call(){ brand.call(); } }
Different types directly inherit this abstract class to call the implementation class of the interface
public class FoldedPhone extends Phone{ public FoldedPhone(Brand brand) { super(brand); } @Override protected void open() { super.open(); System.out.println("Folding style mobile phone"); } @Override protected void close() { super.close(); System.out.println("Folding style mobile phone"); } @Override protected void call() { super.call(); System.out.println("Folding style mobile phone"); } }
public class UpRightPhone extends Phone{ public UpRightPhone(Brand brand) { super(brand); } @Override protected void open() { super.open(); System.out.println("upright style phone"); } @Override protected void close() { super.close(); System.out.println("upright style phone"); } @Override protected void call() { super.call(); System.out.println("upright style phone"); } }
transfer
public class Client { public static void main(String[] args) { HashMap<String,Object> map = new HashMap<>(); map.put("",1); FoldedPhone p1 = new FoldedPhone(new XiaoMi()); p1.open(); p1.call(); p1.close(); System.out.println("------"); FoldedPhone p2 = new FoldedPhone(new Vivo()); p2.open(); p2.call(); p2.close(); System.out.println("======"); UpRightPhone p3 = new UpRightPhone(new XiaoMi()); p3.open(); p3.call(); p3.close(); System.out.println("------"); UpRightPhone p4 = new UpRightPhone(new Vivo()); p4.open(); p4.call(); p4.close(); } }
output
Bridge Mode Notes and Details
1) Separate the abstract from the implementation, which provides great flexibility for the system to separate the abstract from the implementation. This helps the system design hierarchically, resulting in a better structured system.
2) For the high-level part of the system, you only need to know the interface of the abstract part and the implementation part, and the other parts are completed by the specific business.
3) The bridge mode replaces the multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance costs of the system.
4) The introduction of the bridge mode increases the understanding of the system and the difficulty of I design. Since the aggregation relationship is established at the abstraction layer, developers are required to design and program for abstraction
5) The bridge mode requires the correct identification of two independently changing dimensions in the system, so its scope of use has certain limitations, that is, such an application scenario is required.
Learning source: Han Shunping at station B