[GO] Template Method pattern in Java

Introduction

Introducing the design patterns of [GoF](https://ja.wikipedia.org/wiki/Gang of for _ (Information Engineering)) ["Introduction to Design Patterns Learned in the Augmented and Revised Java Language"]( https://www.amazon.co.jp/ Augmented and revised edition Introduction to design patterns learned in Java language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _ Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP) I will summarize about.

TemplateMethod pattern

What is Template Method?

** The pattern that defines the processing framework in the superclass and the concrete implementation in the subclass ** is called the ** Template Method pattern . Of these, the " method that defines the processing framework **" in the superclass is ** TemplateMethod **.

Character

The classes that appear in the class diagram below are used in the TemplateMethod pattern. image.png

Abstract class

Implementation class

Concrete example

As a concrete example, the explanation will be based on "** Procedure manual ", " Procedure manual for work A ", and " Procedure manual for work B **".

Abstract class

-** AbstractManual class ** The AbstractManual class is an abstract class that implements the framework of a routing. Here, ** ʻoperation () method ** is the method (templateMethod) that implements the framework of the work procedure. The other start ()method,work () method, and ʻend () method are declared and called in the ʻoperation () method, but they are not implemented. Specific processing is defined in the subclasses ʻOperationAManual.class and ʻOperationBManual.class`.

AbstractManual.java


package templateMethod;

public abstract class AbstractManual{
	public abstract void start();

	public abstract void work();

	public abstract void end();

	public final void operation() {
		start();
		for (int i = 0; i < 3; i++) {
			work();
		}
		end();
	}
}

Implementation class

-** OperationAManual class ** It implements the abstract methods start (), work () and ʻend (). The constructor receives ** characters ** and outputs them with the work ()` method.

OperationAManual.java


package templateMethod;

public class OperationAManual extends AbstractManual {
	private char ch;

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

	@Override
	public void start() {
		System.out.println("<<Work A start>>");
	}

	@Override
	public void work() {
		System.out.println(ch + "Is processing");
	}

	@Override
	public void end() {
		System.out.println("<<Work A finished>>");
	}
}

-** OperationBManual class ** Like ʻOperationAManual.class, it implements the abstract methods start () , work () , and ʻend (), but the implementation content is different. ** ** The constructor receives ** string ** and the work () method outputs it.

OperationBManual.java


package templateMethod;

public class OperationBManual extends AbstractManual {
	private String string;

	public OperationBManual(String string) {
		this.string = string;
	}

	@Override
	public void start() {
		System.out.println("**Start work B**");
	}

	@Override
	public void work() {
		System.out.println(string + "Is processing");
	}

	@Override
	public void end() {
		System.out.println("**Work B finished**");
	}
}

Execution class

-** Main class ** It instantiates ʻOperationAManual and ʻOperationBManual and calls ʻoperation ()` of TemplateMethod.

Main.java


package templateMethod;

public class Main {
	public static void main(String[] args) {
		AbstractManual d1 = new OperationAManual('F');
		AbstractManual d2 = new OperationBManual("HelloWorld");
		d1.operation();
		d2.operation();
	}
}

Execution result

The result of executing Main.java is as follows. I called the same ʻoperation ()`, but I can see that the output is different.

Execution result


<<Work A start>>
Processing F
Processing F
Processing F
<<Work A finished>>
**Start work B**
Processing Hello World
Processing Hello World
Processing Hello World
**Work B finished**

merit

By utilizing the TemplateMethod pattern, the processing framework can be standardized. By doing this, it is possible to prevent multiple workers from creating from the framework of processing as they like, and the parts that should be uniform are not uniform.

Summary

You learned about the Template Method pattern, which defines the skeleton of the process. The sample code is uploaded below, so please refer to it if you like.

-TemplateMethod sample code

In addition, other design patterns are summarized below, so please refer to them as well.

-[Updated from time to time] Summary of design patterns in Java

References

-[Introduction to Design Patterns Learned in the Augmented and Revised Java Language](https://www.amazon.co.jp/ Introduction to Design Patterns Learned in the Augmented and Revised Java Language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP)

Recommended Posts

Template Method pattern in Java
Facade pattern in Java
Singleton pattern in Java
Flyweight pattern in Java
Observer pattern in Java
Iterator pattern in Java
[Details] Template Method pattern
Decorator pattern in Java
Design Pattern #Template Method
Prototype pattern in Java
Proxy pattern in Java
Learn the design pattern "Template Method" in Python
Python Design Pattern --Template method
Chain of Responsibility pattern in Java
Learn the design pattern "Factory Method" in Python
[Gang of Four] Design pattern learning --Template Method
Singleton pattern in Python
Linux permissions in Java
Use DataFrame in Java
Design Pattern #Factory Method
Visitor pattern in Python
Simplex method (simplex method) in Python
Private method in python
I wrote a design pattern in kotlin Template edition
Override save method in django-models
Implement method chain in Python
Suppressing method overrides in Python
Show Django ManyToManyField in Template
Detect and process signals in Java.
Use jinja2 template in excel file
Implemented label propagation method in Python
Implemented bubble sort in Java (BubbleSort)
GoF java design pattern rough summary