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:
- List of employee cards to send data to the third party app;
- List of employee cards to collect biometry;
- Interval of numbers.
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:
- Create one
abstract classto hold the name convention. And to write the content to a file. - Create a
factory classthat will know all the types of files (factory is a good pattern to use here?). - Implement concrete classes to the types of files to define which content will be written.
More or less what you said:
1-The template method pattern for writing the file. I am thinking something like this:
2- You would have different implementations of these, dispensed by a factory.