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.
** 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 **.
The classes that appear in the class diagram below are used in the TemplateMethod pattern.
ConcreateClass
.
Therefore, methods other than template methods are abstract methods.As a concrete example, the explanation will be based on "** Procedure manual ", " Procedure manual for work A ", and " Procedure manual for work B **".
-** 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();
}
}
-** 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**");
}
}
-** 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();
}
}
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**
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.
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.
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
-[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