directory
- What is the bridge mode?
- Usage scenarios
- Code implementation
What is the bridge mode?
- The bridging pattern separates the abstract part from its implementation part so that they can all change independently.
- Concrete solution of bridge pattern: Bridge pattern solves multidimensional scaling problem by changing inheritance to composition. Specifically, one of the dimensions is extracted and made into a separate class hierarchy, so that objects of this new hierarchy can be referenced in the initial class (which is what Bridges are called in the bridging pattern), so that a class does not have to have all of its states and behaviors.
- Abstract and implementation parts: The abstract and implementation parts here have nothing to do with abstract implementations in programming languages. The abstract part is a high-level control layer of some entity that does not do any concrete work on its own and needs to delegate work to the implementation part layer (also known as the platform). In a real program, the abstract part is the graphical user interface (GUI), while the implementation part is the underlying operating system code (API). The GUI layer calls the API layer to respond to various user actions. GUI, and API can be independently extended.
Usage scenarios
- A system needs to add more flexibility between the abstract orange colors and the embodied personas that are built. Avoiding static inheritance between the two levels, the bridge pattern allows them to establish an association at the abstraction level.
- A class has two dimensions that vary independently, and both dimensions need to be extended.
- The bridge pattern can also be used for systems where inheritance is not desirable or where the number of system classes increases dramatically due to multi-level inheritance.
Code implementation
- Implement partial abstract interfaces
public interface CoffeeAdditives {
String addSomething(a);
}
Copy the code
- Implement partial implementation
public class Milk implements CoffeeAdditives{
@Override
public String addSomething(a) {
return "Milk"; }}public class Sugar implements CoffeeAdditives {
@Override
public String addSomething(a) {
return "Sugar"; }}Copy the code
- Implementation of the abstract part
public abstract class Coffee {
protected CoffeeAdditives mCoffeeAdditives;
public Coffee(CoffeeAdditives coffeeAdditives) {
mCoffeeAdditives = coffeeAdditives;
}
abstract void makeCoffee(a);
}
Copy the code
- Abstract part multisubclass
public class SmallCoffee extends Coffee {
public SmallCoffee(CoffeeAdditives coffeeAdditives) {
super(coffeeAdditives);
}
@Override
void makeCoffee(a) {
System.out.println("Make small cups." +mCoffeeAdditives.addSomething()+ "Coffee"); }}public class MiddleCoffee extends Coffee {
public MiddleCoffee(CoffeeAdditives coffeeAdditives) {
super(coffeeAdditives);
}
@Override
void makeCoffee(a) {
System.out.println("Make medium cups." + mCoffeeAdditives.addSomething() + "Coffee"); }}public class LargeCoffee extends Coffee {
public LargeCoffee(CoffeeAdditives coffeeAdditives) {
super(coffeeAdditives);
}
@Override
void makeCoffee(a) {
System.out.println("Make a mug." + mCoffeeAdditives.addSomething() + "Coffee"); }}Copy the code
- Client call
public class MakeCoffeeTest {
@Test
public void makeCoffee(a) {
new SmallCoffee(new Milk()).makeCoffee();
new SmallCoffee(new Sugar()).makeCoffee();
new MiddleCoffee(new Milk()).makeCoffee();
new MiddleCoffee(new Sugar()).makeCoffee();
new LargeCoffee(new Milk()).makeCoffee();
new LargeCoffee(newSugar()).makeCoffee(); }} Make small milky coffee make small sweetened coffee make medium milky coffee make medium sweetened coffee make large milky coffee make large sweetened coffeeCopy the code