Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:05
Show Gist options
  • Save up1/4471db56db6d5add1a85 to your computer and use it in GitHub Desktop.
Save up1/4471db56db6d5add1a85 to your computer and use it in GitHub Desktop.
Demo :: Using Template Method Pattern
public abstract class AbstractCSVReader<T> {
public List<T> getAll(File file) throws Exception {
List<T> returnData = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = reader.readLine();
while (line != null && !"".equals(line.trim())) {
String[] tokens = line.split("\\s*,\\s*");
T data = unmarshall(tokens);
returnData.add(data);
line = reader.readLine();
}
}
return returnData;
}
public abstract T unmarshall(String[] tokens);
}
public class CustomerCSVReader {
public List<Customer> getAll(File customersFile) throws Exception {
List<Customer> customers = new ArrayList<Customer>();
try (BufferedReader reader = new BufferedReader(new FileReader(customersFile))) {
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
Customer customer = new Customer(Integer.parseInt(tokens[0]), tokens[1], tokens[2]);
customers.add(customer);
line = reader.readLine();
}
}
return customers;
}
}
public class CustomerCSVReader {
public List<Customer> getAll(File file) throws Exception {
List<Customer> returnData = new ArrayList<Customer>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = reader.readLine();
while (line != null && !"".equals(line.trim())) {
String[] tokens = line.split("\\s*,\\s*");
Customer customer = unmarshall(tokens);
returnData.add(customer);
line = reader.readLine();
}
}
return returnData;
}
private Customer unmarshall(String[] tokens) {
Customer customer = new Customer(Integer.parseInt(tokens[0]), tokens[1], tokens[2]);
return customer;
}
}
public class CustomerCSVReader extends AbstractCSVReader<Customer> {
@Override
public Customer unmarshall(String[] tokens) {
Customer customer = new Customer(Integer.parseInt(tokens[0]), tokens[1], tokens[2]);
return customer;
}
}
public class ProductCSVReader {
public List<Product> getAll(File customersFile) throws Exception {
List<Product> products = new ArrayList<Product>();
try (BufferedReader reader = new BufferedReader(new FileReader(customersFile))) {
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
Product product = new Product(Integer.parseInt(tokens[0]), tokens[1], Double.parseDouble(tokens[2]));
products.add(product);
line = reader.readLine();
}
}
return products;
}
}
public class ProductCSVReader extends AbstractCSVReader<Product> {
@Override
public Product unmarshall(String[] tokens) {
Product product = new Product(Integer.parseInt(tokens[0]), tokens[1], Double.parseDouble(tokens[2]));
return product;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment