This is the 15th day of my participation in the August Text Challenge.More challenges in August

The bridge model

Separate the abstract parts from the implementation parts so that they can be changed independently. It is realized by using combinatorial relation instead of inheritance relation, thus reducing the coupling degree of abstraction and realization of these two variable dimensions.Copy the code

advantages

  • Abstraction and implementation separation, strong ability to expand
  • In line with the open and close principle
  • In line with the principle of composite reuse
  • The implementation details are transparent to the customer

disadvantages

Since the aggregation relationship is established at the abstraction level, developers are required to design and program for abstraction, and can correctly identify the two independent changing dimensions in the system, which increases the difficulty of understanding and designing the system.

structure

  1. Abstraction character: Defines the abstract class and contains a reference to the implemented object.
  2. The Refined Abstraction character: is a subclass of the Abstraction character that implements the business methods in the parent class and implements the business methods in the character through composite relationship calls.
  3. Implementor Role: Defines an interface that implements the role for extended abstract role invocation.
  4. Concrete Implementor: Provides the Concrete implementation of the interface for implementing the role.

demo

counter-examples

// Define the interface for making clothes
public interface clothes {
    void make(a);
}
/ / implementation class
public class JacketClothes implements clothes {
    @Override
    public void make(a) {
        System.out.println("Make a coat!); }}// Add a brand
public class LiNingClothes extends JacketClothes {
    @Override
    public void make(a) {
        System.out.println("Li Ning: Make tops!"); }}public class ErkeClothes extends JacketClothes {
    @Override
    public void make(a) {
        System.out.println("Hongxing Erke brand: make tops!"); }}Copy the code

As can be seen from the above code, there will be serious problems in realizing the production of brand clothes through inheritance:

If there are multiple brands, multiple categories need to be generated. If there are multiple clothing categories for a brand, multiple categories need to be generated

As a result, our code does not scale well and does not adhere to the open and close principle

Let’s take a look at the implementation via bridge mode

Is case

1. Define brand dimension abstract classes

public interface Brand {
	void info(a);
}
Copy the code

Define the brand

public class LiNingBrand implements Brand {
    @Override
    public void info(a) {
        System.out.print(Li Ning brand:); }}public class ErkeBrand implements Brand {
    @Override
    public void info(a) {
        System.out.print(Hongxing Erke Brand:); }}Copy the code

Define the product abstract class

public abstract class clothes {
    private Brand brand;
    public clothes(Brand brand) {
        this.brand = brand;
        this.brand.info();
    }
    protected abstract void make(a);
}
Copy the code

4. Define product implementation classes

public class JacketClothes extends clothes {
    public JacketClothes(Brand brand) {
        super(brand);
    }
    @Override
    public void make(a) {
        System.out.println("Make a coat!); }}public class PantsClothes extends clothes {
    public PantsClothes(Brand brand) {
        super(brand);
    }
    @Override
    public void make(a) {
        System.out.println("Make pants!"); }}Copy the code

5. Client

public class Client {
    public static void main(String[] args) {
        Clothes liNingJacket = new JacketClothes(new LiNingBrand());
        liNingJacket.make(); // Li Ning: Make tops!
        Clothes erkeJacket = new JacketClothes(new ErkeBrand());
        erkeJacket.make(); // Hongxing Erke brand: make tops!
        Clothes liNingPants = new PantsClothes(new LiNingBrand());
        liNingPants.make(); // Li Ning: Make pants!
        Clothes erkePants = new PantsClothes(new ErkeBrand());
        erkePants.make(); // Hongxing Erke brand: make pants!}}Copy the code

conclusion

1. Realize the separation of abstraction and implementation and improve the flexibility of the system

2. Replace multi-level inheritance, reduce the number of classes, and improve the scalability of the system

3, to meet the single responsibility and open and close principle

4. Multiple dimensions can be changed independently without affecting other dimensions