mirror of
https://github.com/jlengrand/design-patterns.git
synced 2026-03-10 08:11:17 +00:00
Creates an example of a template method pattern
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package nl.lengrand.patterns.template;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BinReportCreator extends ReportCreator{
|
||||
|
||||
@Override
|
||||
protected List<Integer> readValuesFromInput(File file) throws IOException {
|
||||
String valuesAsString = new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII);
|
||||
|
||||
return Arrays.stream(valuesAsString.split(""))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package nl.lengrand.patterns.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class BinaryFileMaker {
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
Path path = Paths.get("src/main/resources/template.bin");
|
||||
byte[] bytes = "123456".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
Files.write(path, bytes);
|
||||
System.out.println("Successfully written data to the file");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package nl.lengrand.patterns.template;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CsvReportCreator extends ReportCreator {
|
||||
|
||||
@Override
|
||||
protected List<Integer> readValuesFromInput(File file) throws IOException {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String[] values = br.readLine().split(",");
|
||||
return Arrays.stream(values)
|
||||
.map(v -> v.trim())
|
||||
.map(Integer::parseInt)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/main/java/nl/lengrand/patterns/template/README.md
Normal file
10
src/main/java/nl/lengrand/patterns/template/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Template method pattern
|
||||
|
||||
From [Wikipedia](https://en.wikipedia.org/wiki/Template_method_pattern).
|
||||
|
||||
The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps.
|
||||
The intent of the template method is to define the overall structure of the operation, while allowing subclasses to refine, or redefine, certain steps. The template method pattern is an example of inversion of control.
|
||||
|
||||
This pattern is useful to reduce code duplication for variations of the same algorithm.
|
||||
|
||||
Read and run [ReportCreation](ReportCreation.java) to see a live example.
|
||||
@@ -0,0 +1,27 @@
|
||||
package nl.lengrand.patterns.template;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReportCreation {
|
||||
public static void main(String[] args) throws IOException {
|
||||
ReportCreation reportCreation = new ReportCreation();
|
||||
File csvFile = reportCreation.getFileFromName("template.csv");
|
||||
File binFile = reportCreation.getFileFromName("template.bin");
|
||||
|
||||
System.out.println("Creating report from CSV file");
|
||||
CsvReportCreator csvReportCreator = new CsvReportCreator();
|
||||
csvReportCreator.createReport(csvFile);
|
||||
|
||||
System.out.println("Creating report from binary file");
|
||||
BinReportCreator binReportCreator = new BinReportCreator();
|
||||
binReportCreator.createReport(binFile);
|
||||
|
||||
System.out.println("Done");
|
||||
}
|
||||
|
||||
private File getFileFromName(String fileName){
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
return new File(classLoader.getResource(fileName).getFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package nl.lengrand.patterns.template;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class ReportCreator {
|
||||
|
||||
final void createReport(File filePath) throws IOException {
|
||||
List<Integer> values = readValuesFromInput(filePath);
|
||||
checkValues(values);
|
||||
printValues(values);
|
||||
}
|
||||
|
||||
private void printValues(List<Integer> values) {
|
||||
System.out.println("=================");
|
||||
System.out.println("Values are:");
|
||||
System.out.println(values.stream()
|
||||
.map(v -> v.toString())
|
||||
.collect(Collectors.joining(",")));
|
||||
System.out.println("End of report");
|
||||
System.out.println("=================");
|
||||
}
|
||||
|
||||
protected abstract List<Integer> readValuesFromInput(File file) throws IOException;
|
||||
|
||||
private void checkValues(List<Integer> values){
|
||||
if(values == null || values.stream().anyMatch( v -> v < 0)){
|
||||
throw new InputMismatchException("Expecting all values to be positive");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/main/resources/template.bin
Normal file
1
src/main/resources/template.bin
Normal file
@@ -0,0 +1 @@
|
||||
123456
|
||||
1
src/main/resources/template.csv
Normal file
1
src/main/resources/template.csv
Normal file
@@ -0,0 +1 @@
|
||||
1, 2, 3, 4, 5, 6
|
||||
|
Reference in New Issue
Block a user