design patterns : template

This commit is contained in:
haerong22
2021-12-25 16:46:15 +09:00
parent bb620d4b9f
commit fabe51fa21
11 changed files with 177 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
1
2
3
4
5

View File

@@ -0,0 +1,10 @@
package template.after.template_callback;
public class Client {
public static void main(String[] args) {
FileProcessor fileProcessor = new FileProcessor("number.txt");
int result = fileProcessor.process(Integer::sum);
System.out.println(result);
}
}

View File

@@ -0,0 +1,26 @@
package template.after.template_callback;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileProcessor {
private String path;
public FileProcessor(String path) {
this.path = path;
}
public int process(Operator operator) {
try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
int result = 0;
String line = null;
while((line = reader.readLine()) != null) {
result = operator.getResult(result, Integer.parseInt(line));
}
return result;
} catch (IOException e) {
throw new IllegalArgumentException(path + "에 해당하는 파일이 없습니다.", e);
}
}
}

View File

@@ -0,0 +1,6 @@
package template.after.template_callback;
public interface Operator {
int getResult(int result, int line);
}

View File

@@ -0,0 +1,12 @@
package template.after.template_method;
public class Client {
public static void main(String[] args) {
FileProcessor fileProcessor = new Plus("number.txt");
System.out.println(fileProcessor.process());
fileProcessor = new Multiply("number.txt");
System.out.println(fileProcessor.process());
}
}

View File

@@ -0,0 +1,28 @@
package template.after.template_method;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public abstract class FileProcessor {
private String path;
public FileProcessor(String path) {
this.path = path;
}
public int process() {
try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
int result = 0;
String line = null;
while((line = reader.readLine()) != null) {
result = getResult(result, Integer.parseInt(line));
}
return result;
} catch (IOException e) {
throw new IllegalArgumentException(path + "에 해당하는 파일이 없습니다.", e);
}
}
protected abstract int getResult(int result, int line);
}

View File

@@ -0,0 +1,13 @@
package template.after.template_method;
public class Multiply extends FileProcessor {
public Multiply(String path) {
super(path);
}
@Override
protected int getResult(int result, int line) {
return result * line;
}
}

View File

@@ -0,0 +1,15 @@
package template.after.template_method;
import template.after.template_method.FileProcessor;
public class Plus extends FileProcessor {
public Plus(String path) {
super(path);
}
@Override
protected int getResult(int result, int line) {
return result + line;
}
}

View File

@@ -0,0 +1,10 @@
package template.before;
public class Client {
public static void main(String[] args) {
FileProcessor fileProcessor = new FileProcessor("number.txt");
int result = fileProcessor.process();
System.out.println(result);
}
}

View File

@@ -0,0 +1,26 @@
package template.before;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileProcessor {
private String path;
public FileProcessor(String path) {
this.path = path;
}
public int process() {
try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
int result = 0;
String line = null;
while((line = reader.readLine()) != null) {
result += Integer.parseInt(line);
}
return result;
} catch (IOException e) {
throw new IllegalArgumentException(path + "에 해당하는 파일이 없습니다.", e);
}
}
}

View File

@@ -0,0 +1,26 @@
package template.before;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class MultiplyFileProcessor {
private String path;
public MultiplyFileProcessor(String path) {
this.path = path;
}
public int process() {
try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
int result = 0;
String line = null;
while((line = reader.readLine()) != null) {
result *= Integer.parseInt(line);
}
return result;
} catch (IOException e) {
throw new IllegalArgumentException(path + "에 해당하는 파일이 없습니다.", e);
}
}
}