[Java] Bridge pattern

2 minute read

What is Bridge pattern

It bridges between “function class hierarchy” and “implementation class hierarchy”. Wikipedia says it aims to extend the class in multiple directions by providing a “bridge” class.

The role of abstraction

The top-level class in the “Class hierarchy of features”. A class in which only the basic functions are described using the methods of the Implementor role. This class holds the role of Implementor.

``’’ java package bridge;

public class Display { private DisplayImpl impl;

public Display(DisplayImpl impl) { this.impl = impl; }

public void open() { impl.rawOpen(); }

public void print() { impl.rawPrint(); }

public void close() { impl.rawClose(); }

public final void display() { open(); print(); close(); } }


The role of ###Refined Abstraction (improved abstraction)
A role that adds functions to the Abstraction role.

``'' java
package bridge;

public class CountDisplay extends Display {
public CountDisplay(DisplayImpl impl) {
super(impl);
}

public void multiDisplay(int times) {
open();
for (int i = 0; i <times; i++) {
print();
}
close();
}
}

Implementer role

Top-level class in the Implementation Class Hierarchy. Role that specifies the method for implementing the interface of Abstraction role.

``’’ java package bridge;

public abstract class DisplayImpl { public abstract void rawOpen(); public abstract void rawPrint(); public abstract void rawClose(); }


### ConcreteImplementor role
The role that implements the interface of Implementor.

``'' java
package bridge;

public class StringDisplayImpl extends DisplayImpl {
private String string;
private int width;

public StringDisplayImpl(String string) {
this.string = string;
this.width = string.getBytes().length;
}

public void rawOpen() {
printLine();
}

public void rawPrint() {
System.out.println("|" + string + "|");
}

public void rawClose() {
printLine();
}

public void printLine() {
System.out.print("+");
for (int i = 0; i <width; i++) {
System.out.print("-");
}
System.out.println("+");
}
}

Caller

``’’ java package bridge;

public class Main { public static void main(String[] args) { Display d1 = new Display(new StringDisplayImpl(“Hello Japan”)); Display d2 = new CountDisplay(new StringDisplayImpl(“Hello World”)); CountDisplay d3 = new CountDisplay(new StringDisplayImpl(“Hello Universe”)); d1.display(); d2.display(); d3.display(); d3.multiDisplay(5); } }


### Execution result
![Screenshot 2020-09-07 18.58.23.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/197834/1bf48669-d06a-b772-9eef-(02e3a2dcbebf.png)

Add a class that displays the following patterns.
<>
<\*>
<\*\*>
<\*\*\*>

These are the first character → multiple decoration characters → the last character and a line feed, which are repeated multiple times.

When adding a class that operates as above, separate into a class that represents "function" and a class that represents "implementation".

### Class that represents the "function" of gradually increasing the number and displaying
``'' java
package bridge;

public class IncreaseDisplay extends CountDisplay {
// increment
private int step;

public IncreaseDisplay(DisplayImpl impl, int step) {
super(impl);
this.step = step;
}

public void increaseDisplay(int level) {
int count = 0;
for (int i = 0; i <level; i++) {
multiDisplay(count);
count += step;
}
}
}

Class that represents the “implementation” of displaying with ### characters ``’’ java package bridge;

public class CharDisplayImpl extends DisplayImpl { private String firstLetter; private String decoration; private String lastLetter;

public CharDisplayImpl(String firstLetter, String decoration, String lastLetter) { this.firstLetter = firstLetter; this.decoration = decoration; this.lastLetter = lastLetter; }

@Override public void rawOpen() { System.out.print(firstLetter); }

@Override public void rawPrint() { System.out.print(decoration); }

@Override public void rawClose() { System.out.println(lastLetter); } }


### Caller

``'' java
package bridge;

public class Main {
public static void main(String[] args) {
Display d1 = new Display(new StringDisplayImpl("Hello Japan"));
Display d2 = new CountDisplay(new StringDisplayImpl("Hello World"));
CountDisplay d3 = new CountDisplay(new StringDisplayImpl("Hello Universe"));
IncreaseDisplay d4 = new IncreaseDisplay(new CharDisplayImpl("<", "*", ">"), 2);
d1.display();
d2.display();
d3.display();
d3.multiDisplay(5);
d4.increaseDisplay(3);
}
}

Execution result

Screenshot 2020-09-08 11.38.58.png

https://github.com/aki0207/bridge

I referred to this. Introduction to Design Patterns Learned from the Enhanced and Revised Java Language

Tags:

Updated: