define

Separate the abstract part from the implementation part so that they can change independently.

A class has two dimensions that change independently, and both dimensions need to be extended.

nature

Separate abstraction from implementation

His role

  • Abstraction

    This role is at the top of the class’s functional hierarchy. He defines the basic functionality using the implementor role approach, which holds an instance of the implementor role.

  • RefinedAbstraction

    A character with new functions added to an abstract character.

  • Implementor

    This role is at the top of the class’s implementation hierarchy. It defines interfaces for implementing abstract roles.

  • ConcreteImplementor

    This role is responsible for implementing the interface defined in the implementer role.

Code sample

*/ public Abstract class Coffee {protected coffee_complementary; */ Public Abstract class Coffee {protected coffee_complementary; public Coffee(CoffeeAdditives coffeeAdditives) { this.coffeeAdditives = coffeeAdditives; } public void makeCoffee();} public void makeCoffee(); } /** * public class LargeCoffee extends Coffee{public LargeCoffee(coffeeIpe) { super(coffeeAdditives); } @override public void makeCoffee() {system.out.println ("大 体 "+ coffeeIpe +" coffee "); ** * public class extends Coffee{public SmallCoffee(coffeee_type coffeee_type) { super(coffeeAdditives); } @override public void makeCoffee() {system.out.println (" small cup "+ coffeeIpe +" coffee "); }} /** * realizations */ public abstract Class coffeeIpe {/** * What exactly to add to the coffee is determined by the subclass */ public abstract String addSomething(); } /** * public class coffarycoffee extends coffeeType {@override public String addSomething() { Return "taste "; }} public class extends coffeeType {@override public String addSomething() { Return "sugar "; Public class Main {public static void Main (String[] args){public static void Main (String[] args){public static void Main (String[] args) OrdinaryCoffee(); SugerCoffee = new SugerCoffee(); LargeCoffee = new LargeCoffee(ordinaryCoffee); largeCoffeeOrdinary.makeCoffee(); /// SmallCoffee SmallCoffee = new SmallCoffee(ordinaryCoffee); smallCoffeeOrdinary.makeCoffee(); // LargeCoffee largeCoffeeSuger = new LargeCoffee(sugerCoffee); largeCoffeeSuger.makeCoffee(); // smallCoffeeSuger smallCoffeeSuger = new SmallCoffee(sugerCoffee); smallCoffeeSuger.makeCoffee(); }}Copy the code

The results

Large bridge1.OrdinaryCoffee@6ce253f1 small bridge1.OrdinaryCoffee@6ce253f1 large bridge1.SugerCoffee@e9e54c2 coffee Small bridge1.SugerCoffee@e9e54c2 coffeeCopy the code

advantages

  • Separating the abstraction from the implementation, separating the abstraction from the implementation, and defining interfaces separately, helps to layer the system.
  • Better extensibility so that the abstract and implementation parts can be extended separately without affecting each other.
  • Dynamically switch implementations, and you can dynamically select and use specific implementations.
  • The number of subclasses can be reduced. If there are two varying dimensions, a large number of subclasses will be generated when inheriting, while the number of subclasses can be significantly reduced by bridging.

disadvantages

  • It is not easy to design and requires certain experience for developers.

When to use

The bridge pattern is also known as the bridge pattern, where the function of the bridge is to connect the abstract part and the implementation part, but in fact, any multi-dimensional class or multiple tree class coupling can be decoupled using the bridge pattern.

If a system needs to add more flexibility between constructed abstract and embodied roles, avoiding static inheritance relationships between the two levels, the bridge pattern can be used to enable them to establish an association relationship at the abstraction level.

For systems where inheritance is not preferred or where the number of classes increases dramatically due to multi-level inheritance, the bridge pattern can also be considered.