design patterns : template
This commit is contained in:
5
design-pattern/gof/number.txt
Normal file
5
design-pattern/gof/number.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package template.after.template_callback;
|
||||
|
||||
public interface Operator {
|
||||
|
||||
int getResult(int result, int line);
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
10
design-pattern/gof/src/template/before/Client.java
Normal file
10
design-pattern/gof/src/template/before/Client.java
Normal 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);
|
||||
}
|
||||
}
|
||||
26
design-pattern/gof/src/template/before/FileProcessor.java
Normal file
26
design-pattern/gof/src/template/before/FileProcessor.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user