Java Design Patterns —— Bridge

Keywords: Android Java Windows

Java Design Patterns - Bridge

Definition

Bridging mode: Separate Abstract parts from their implementation parts so that they can change independently.

Features:

  1. Three key words: abstraction, realization and decoupling
  2. Bridging mode separates abstract part from implementation part and decouples them
  3. Two Dimensions, Independent Change, Flexible Extension

code implementation

Jane book: Bridging Patterns of Android Design Patterns

  • Here we will take a brief look at the implementation and application of bridge mode through the example of brewing coffee.
public abstract class Coffee {
    protected CoffeeAdditives impl;

    public Coffee (CoffeeAdditives impl) {
        this.impl = impl;
    }

    /**
     * What exactly coffee is is determined by subcategories
     */
    public abstract void makeCoffee();
}
  • Subclasses of coffee, large and small cups, sugar-free operations are added through impl
//Large Cafe
public class LargeCoffee extends Coffee {

    public LargeCoffee(CoffeeAdditives coffeeAdditives) {
        super(coffeeAdditives);
    }

    @Override
    public void makeCoffee() {
        System.out.println("Large cup" + impl.addSomething() + "Coffee");
    }
}
//Small cup of coffee
public class SmallCoffee extends Coffee {

    public SmallCoffee(CoffeeAdditives coffeeAdditives) {
        super(coffeeAdditives);
    }

    @Override
    public void makeCoffee() {
        System.out.println("Small cup" + impl.addSomething() + "Coffee");
    }
}
  • Now let's look at the implementation part, that is, with or without sugar.
public abstract class CoffeeAdditives {

    /**
     * What to add is determined by subclasses
     *
     * @return Additions, such as sugar
     */
    public abstract String addSomething();
}
  • Sugared and unsweetened subclasses
//Sugar addition
public class Sugar extends CoffeeAdditives {

    @Override
    public String addSomething() {
        return "Sugar addition";
    }
}
//Without sugar, this is the original taste.
public class Ordinary extends CoffeeAdditives {

    @Override
    public String addSomething() {
        return "Original flavor";
    }
}
  • The abstract part and the implementation part are all finished. Now let's see how to make coffee.
//Original flavor
Ordinary ordinary = new Ordinary();

//Sugar addition
Sugar sugar = new Sugar();

//Big cup of coffee, original taste
LargeCoffee largeCoffee = new LargeCoffee(ordinary);
largeCoffee.makeCoffee();

//Small cup of coffee, original taste
SmallCoffee smallCoffee = new SmallCoffee(ordinary);
smallCoffee.makeCoffee();

//Large cup of coffee with sugar
LargeCoffee largeCoffeeSugar = new LargeCoffee(sugar);
largeCoffeeSugar.makeCoffee();

//Small cup of coffee with sugar
SmallCoffee smallCoffeeSugar = new SmallCoffee(sugar);
smallCoffeeSugar.makeCoffee();
  • Output log

Big cup of original coffee
A small cup of original coffee
A large cup of sweetened coffee
A small cup of sweetened coffee

From the above examples, we can see that the bridge pattern can separate abstraction from implementation, and the extension is flexible. For example, we need to add a medium cup of coffee, just in the abstract part, that is, to write a subclass of Coffee MiddleCoffee, while in the implementation part, Coffee Additives are not affected. If coffee needs salt or something, it's as simple as that.

Appearance mode usage

Advantage:
1. Separation of abstraction and implementation.
2. Excellent expansion ability.
3. Implementation details are transparent to customers.

Disadvantage: The introduction of bridging mode will increase the difficulty of understanding and designing the system. Because aggregation association is based on abstraction layer, developers are required to design and program for abstraction.

Appearance Models in Android

AbsListView and ListAdapter are bridging modes.

In addition, there is a bridge mode between Windows and Windows Manager.

Thank

segmentfault: Bridging mode
New Bird Course: Bridging mode
Baidu Encyclopedia: Bridging mode
Blog Garden: Bridge Model in JAVA and Model
Jane book: Bridging Patterns of Android Design Patterns

Posted by FFEMTcJ on Thu, 02 May 2019 16:00:37 -0700