Design pattern ~ Bridge ~

1.First of all

Here is a summary of the ** Bridge pattern ** in the GoF design pattern.

2. What is the Bridge 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 **.

3. Sample class diagram

Bridge.PNG

4. Sample program

This program displays the entered character string a specified number of times and a random number of times.

4-1. Display class

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

4-2. CountDisplay class

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

4-3. RandomCountDisplay class

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

4-4. DisplayImpl class

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

4-5. StringDisplayImpl class

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

4-6. Main class

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

4-7. Execution result

+------------+
|Display Test|
+------------+
+-----------------+
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
|CountDisplay Test|
+-----------------+
+-----------------------+
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
|RandomCountDisplay Test|
+-----------------------+

5. Benefits

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.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Bridge ~
Bridge Pattern
Bridge pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Command Pattern
Strategy pattern
Iterator Pattern
Adapter Pattern
Strategy Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -