Last active
August 29, 2015 14:05
-
-
Save up1/4471db56db6d5add1a85 to your computer and use it in GitHub Desktop.
Demo :: Using Template Method Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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