I would suggest using a Provider and a Strategy pattern, in conjunction with a Factory Pattern.
If I understand correctly, you want the following:
1. Generating multiple file types, say n types from {T1, ... Tn}
2. The number of types is not fixed - there is a possibility of increase in that number in future.
3. You want to make sure that the modification done to the application is minimal in future if a new type is needed.
Here is my approach.
- Configure the type of possible file type/extension as a property
- The implementation classes for the provider contain the code for any file type conversions as per the format based on conversion and modification strategy.
- The Factory method is used to get the provider.
DETAILS:-
PFB the code
Provider:
public interface ConversionProvider {
public SupportedFile convert(String inputFilePath);
public void register();
}
public class ExcelProvider implements ConversionProvider{
private ZipStrategy zipStrategy;
@Override
public ExcelFile convert(String inputFilePath) {
// TODO Auto-generated method stub
return new ExcelFile();
}
@Override
public void register() {
// TODO Auto-generated method stub
}
}
Factory:
public abstract class ConversionTypeFactory {
protected ConversionProvider provider;
/**
* Conversion Provider for the input Type.
* @return
*/
public abstract ConversionProvider getProvider();
}
public class ExcelTypeFactory extends ConversionTypeFactory{
@Override
public ConversionProvider getProvider() {
// TODO Auto-generated method stub
provider = new ExcelProvider();
return provider;
}
}
File Hierarchy
public class SupportedFile {
protected String fullQualifiedPath;
}
public class ExcelFile extends SupportedFile{
}
Strategy
public class FileStrategy {
}
public class ZipStrategy extends FileStrategy{
private String compressionMode;
private String encryptionType;
}
public class SplitStrategy extends FileStrategy{
private int defaultBlocksCount;
}
Invocation:
public class UseProvider {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name="Excel"; //Assume to be read from property or assigned dynamically
ExcelFile file;
String inputFilePath="";
ConversionTypeFactory factory;
if(name.equals("Excel")){
factory = new ExcelTypeFactory();
file= (ExcelFile) factory.getProvider().convert(inputFilePath);
}
}
}
Q: Is keeping three patterns mixed up with each other, an overkill?
Ans:- The answer to this question lies in your specific requirements. I have considered a complicated system wherein it will be best to break down each and every concern related to your requirement. It will ease maintenance, packaging and dependency management.
Q: What scenarios will I address keeping three patterns ?
Ans:-
The scenario of scale-ability, and segregation, and ease of use.
If we need a new file type, add properties, add a factory subclass, add a conversion provider, which can use a pre-defined strategy. Your strategies are never mixed up with your providers. Your providers are never mixed up with your Factories.
Future Cases and Expansion:-
If some file merging requirement is needed in future, simply add a strategy sub class, and get going. If the requirement is such that FileOperations need to be expanded to fit an ETL ecosystem, the providers work as a service easily (names of course have to be more explicit in that case), which can be registered in the system, with your factories being re-used.
With this approach, you are free to package your Services and Strategies in a separate jar and simply adding a dependency in a related project.
Please let me know if this serves your purpose.