Bridge pattern

What is the Bridge pattern?

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 role of Abstraction

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();
	}
}

The role of Refined Abstraction

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 role of Implementor

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();
}

The role of Concrete Implementor

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("+");
	}
}

Caller

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

スクリーンショット 2020-09-07 18.58.23.png

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".

A class that represents the "function" of displaying more and more times

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;
		}
	}
}

A class that represents an "implementation" that displays in characters

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

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

スクリーンショット 2020-09-08 11.38.58.png

https://github.com/aki0207/bridge

I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language

Recommended Posts

Bridge Pattern
Bridge pattern
Design pattern ~ Bridge ~
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
java callback pattern
Design pattern ~ Proxy ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Facade ~
Design pattern ~ Mediator ~
Template Method pattern
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Java pattern memo