[JAVA] Template Method pattern

Roughly Template Method

A design pattern in which a superclass defines the processing framework and subclasses define the specific content is called the Template Method. There are two roles that appear, the role of Abstract Class and the role of Concrete Class.

The role of Abstract Class

The AbstractClass role implements the template method. Also, declare the abstract method used in the template method.

package templateMethod;

public abstract class AbstractDisplay {
	public abstract void open();
	public abstract void print();
	public abstract void close();

	public final void display() {
		open();
		for(int i = 0; i < 5; i++) {
			print();
		}
		close();
	}
}

The role of Concrete Class

Concretely implement the abstract method defined in the AbstractClass role. The method implemented here is called from the template method that plays the role of Abstract.

package templateMethod;

public class CharDisplay extends AbstractDisplay {
	private char ch;

	 public CharDisplay(char ch) {
		 this.ch = ch;
	}

	@Override
	public void open() {
		System.out.print("<<");
	}

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

	@Override
	public void close() {
		System.out.println(">>");
	}
}
package templateMethod;

public class StringDisplay extends AbstractDisplay{
	private String string;
	private int width;

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

	@Override
	public void open() {
		printLine();
	}

	@Override
	public void print() {
		System.out.println("|" + string + "|");
	}

	@Override
	public void close() {
		printLine();
	}

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

Caller

package templateMethod;

public class Main {

	public static void main(String[] args) {

		AbstractDisplay cd = new CharDisplay('c');
		cd.display();

		AbstractDisplay sd = new StringDisplay("Hello,World");
		sd.display();

	}
}
//result
//<<ccccc>>
//+-----------+
//|Hello,World|
//|Hello,World|
//|Hello,World|
//|Hello,World|
//|Hello,World|
//+-----------+

Logic standardization

The Template Method pattern can be used to standardize logic. In this sample, the algorithm is described in the template method of the superclass, so it is not necessary to describe the algorithm one by one on the subclass side.

If you create multiple ConcreteClasses by copy and paste without using Template Method If you later find a bug in ConcreteClass, you need to fix the bug in all ConcreteClass. If you are using the Template Method pattern, even if you find a bug in the template method All you have to do is modify the template method.

Recommended Posts

Template Method pattern
Template Method Pattern
Design pattern ~ Template Method ~
Ruby design pattern template method pattern memo
Factory Method Pattern
C # chewed design pattern: Template Method
Factory Method pattern
[Design pattern] Common logic with Template Method
Design pattern ~ Factory Method ~
Method
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
to_i method
java (method)
Builder pattern
Bridge Pattern
getRequestDispatcher () method
Command Pattern
merge method
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Map method
include method
Proxy Pattern
Composite Pattern
Abstract method
initialize method
Singleton Pattern
List method
Singleton pattern
Prototype Pattern
puts method
Java method
Class method
Facade Pattern
save! method
getParameter method
[Java] method
Mod template
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
private method
Facade pattern
Visitor Pattern
rails method
Bridge pattern
[Java] method