In one app, I have a task to create files that will be used by a third party. Actually there are three distinct types of content in the files:
For now I have just one class called FileGenerator
(generic, bad name I think) that receives the data and creates a file with some name convention (code of clock, type of file, date and hour).
There's a good design pattern to ensure that the file name convention will remains and to split the generation of files in specific classes for each type of file?
There's a good way to reuse the code that generates the file (don't repeating myself in the specific classes)?
This is part of the existing class:
class FileGenerator {
private List<String> contentOfFile;
private String fileName;
//I - include employees
//C - change employees
//R - remove employees
//B - collect biometry
//N - interval of numbers
private String option;
private void getFileName(){ ... } //this assure the file name convention
public void generate(){ ... } //this generate the file with content
}
What I think so far:
abstract class
to hold the name convention. And to write the content to a file. factory class
that will know all the types of files (factory is a good pattern to use here?). More or less what you said:
1-The template method pattern for writing the file. I am thinking something like this:
public abstract class EmployeeCardFileGenerator {
/**
* @return the generated file name
*/
public abstract String getFileName(/*any params you need to get the file name*/);
/**
* @return the line corresponding to the given data record
*/
public abstract String getLine(EmployeeCardData data);
/**
* @return the header to be appended at the beginning of the file
*/
public abstract String getHeader(/*any header params*/);
/**
* @return the footer to be appended at the end of the file
*/
public abstract String getFooter(/*any footer params*/);
public void generateFile(/*any params*/) {
List<EmployeeCardData> data = queryData();
File f = createFile();
PrintWriter pw = getWriter(f);
pw.println(getHeader());
for(EmployeeCardData ec : data) {
pw.println(getLine(ec));
}
pw.println(getFooter());
cleanup();
}
}
2- You would have different implementations of these, dispensed by a factory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.