It bridges the "functional class hierarchy" and the "implementation class hierarchy". Wikipedia states that the purpose is to extend the class in multiple directions by providing a "bridge" class.
The top class in the "Function Class Hierarchy". A class in which only basic functions are described using the method of Implementor. This class holds the role of Implementor.
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();
}
}
A role that adds functions to the Abstraction role.
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();
}
}
The top class of the "implementation class hierarchy". A role that defines a method for implementing the interface of the Abstraction role.
package bridge;
public abstract class DisplayImpl {
public abstract void rawOpen();
public abstract void rawPrint();
public abstract void rawClose();
}
Specifically, the role of implementing the interface of the role of Implementor.
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("+");
}
}
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);
}
}
Add a class that displays the following pattern. <> <*> <**> <***>
These are the first character → decorative characters multiple times → the last character and line break as one line, which is repeated multiple times.
When adding a class that operates as above, separate it into a class that represents "function" and a class that represents "implementation".
package bridge;
public class IncreaseDisplay extends CountDisplay {
//Number of increase
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;
}
}
}
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);
}
}
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);
}
}
https://github.com/aki0207/bridge
I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language
Recommended Posts