Here is a summary of the ** Bridge pattern ** in the GoF design pattern.
--The English word Bridge means ** bridge **. ――The Bridge pattern is a method that connects two places , just as a bridge in the real world connects this side and the other side of the river. --The two places where the Bridge pattern bridges are the ** feature class hierarchy ** and the ** implementation class hierarchy . - Function class hierarchy ** ・ ・ ・ This is the hierarchy when a superclass has basic functions and new functions are added in subclasses. - Implementation class hierarchy ** ・ ・ ・ This is the hierarchy when the interface is defined by the abstract method in the superclass and the interface is implemented by the concrete method in the subclass. --The GoF design patterns are classified as ** structural design patterns **.
This program displays the entered character string a specified number of times and a random number of times.
It becomes the class hierarchy of the function. This class is used for "display". The impl field of this class becomes the "bridge" of the two class hierarchies.
Display.java
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();
}
}
It becomes the class hierarchy of the function. This class has a function called "Display specified number of times".
CountDisplay.java
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();
}
}
It becomes the class hierarchy of the function. This class has a function called "random number display".
RandomCountDisplay.java
import java.util.Random;
public class RandomCountDisplay extends CountDisplay {
private Random random = new Random();
public RandomCountDisplay(DisplayImpl impl) {
super(impl);
}
public void randomDisplay(int times) {
multiDisplay(random.nextInt(times));
}
}
It becomes the class hierarchy of the implementation. It is a class that defines the method for "display".
DisplayImpl.java
public abstract class DisplayImpl {
public abstract void rawOpen();
public abstract void rawPrint();
public abstract void rawClose();
}
It becomes the class hierarchy of the implementation. It is a class that "displays using a character string".
StringDisplayImpl.java
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();
}
private void printLine() {
System.out.print("+");
for (int i = 0; i < width; i++) {
System.out.print("-");
}
System.out.println("+");
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
Display d = new Display(new StringDisplayImpl("Display Test"));
CountDisplay cd = new CountDisplay(new StringDisplayImpl("CountDisplay Test"));
RandomCountDisplay rcd = new RandomCountDisplay(new StringDisplayImpl("RandomCountDisplay Test"));
d.display();
cd.multiDisplay(5);
rcd.randomDisplay(10);
}
}
+------------+
|Display Test|
+------------+
+-----------------+
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
+-----------------+
+-----------------------+
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
+-----------------------+
As mentioned above, the feature of the Bridge pattern is that it separates the ** functional class hierarchy ** and the ** implementation class hierarchy **. If you separate these two class hierarchies, you can extend each class hierarchy independently. If you want to add a feature, add the class to the feature's class hierarchy. At this time, the implementation class hierarchy does not need to be modified at all. What's more, ** the added functionality will be available in all implementations **. In the sample program, adding the CountDisplay class and RandomCountDisplay class is the function addition. In this way, the Bridge pattern gives you a clear view of class expansion.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts