Design pattern ~ Template Method ~

1.First of all

We will summarize the ** Template Method pattern ** in the GoF design pattern.

2. What is the Template Method pattern?

--Templates are ** thin plastic plates with holes in the shape of letters **. You can see what kind of characters you can write by looking at the Template, but you can't know what kind of characters you will actually get unless you decide on a specific writing instrument. --The Template Method pattern is a method in which the processing framework is defined in the superclass and the specific contents are defined in the subclass **. --The GoF design patterns are classified as ** behavioral design patterns **.

3. Sample class diagram

TemplateMethod.PNG

4. Sample program

It is a program that displays the name, offensive power, and defensive power of the monster.

4-1. AbstractMonster class

This is a template class.

AbstractMonster.java


public abstract class AbstractMonster {

	public String name;
	public abstract int getAttack();
	public abstract int getDefense();

	public final void showInfo() {
		System.out.print("name:");
		System.out.println(name);
		System.out.print("Offensive power:");
		System.out.println(getAttack());
		System.out.print("Defensive power:");
		System.out.println(getDefense());
		System.out.println();
	}
}

4-2. Slime class

A class that implements the methods defined in the AbstractMonster class.

Slime.java


public class Slime extends AbstractMonster {

	public Slime(String name) {
		this.name = name;
	}

	public int getAttack() {
		return 15;
	}

	public int getDefense() {
		return 10;
	}
}

4-3. Dragon class

A class that implements the methods defined in the AbstractMonster class.

Dragon.java


public class Dragon extends AbstractMonster {

	public Dragon(String name) {
		this.name = name;
	}

	public int getAttack() {
		return 60;
	}

	public int getDefense() {
		return 45;
	}
}

4-4. Main class

This class performs the main processing.

Main.java


public class Main {
	public static void main(String[] args) {
		AbstractMonster slime = new Slime("Slime-kun");
		AbstractMonster dragon = new Dragon("Dragon");
		slime.showInfo();
		dragon.showInfo();
	}
}

4-5. Execution result

Name: Slime
Attack power: 15
Defensive power: 10

Name: Dragon
Attack power: 60
Defensive power: 45

5. Benefits

In the Template Method pattern, the algorithm is described by the template method of the superclass, so it is not necessary to describe the algorithm one by one on the subclass side. For example, if you have created similar classes, Class1, Class2, Class3 ... without using the Template Method pattern, and a bug is found in Class1, the bug will be reflected in Class2, Class3 ... Must be. If you create with the Template Method pattern, even if a bug is found in the template method, you only have to fix the template method.

  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 ~ Template Method ~
Template Method pattern
Template Method Pattern
C # chewed design pattern: Template Method
Design pattern ~ Factory Method ~
[Design pattern] Common logic with Template Method
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Java beginner design pattern (Factory Method 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 ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
Item 51: Design method signatures carefully
[Design pattern] Java core library
Method
Introduction to Design Patterns (Factory Method)
Application example of design pattern (No. 1)