1

I have a problem in hand where I have to generate multiple type of file output like PDF, Excel, Text file etc. with the data depending on the user input.

I am trying to implement a Design Pattern that will help me to get rid of the design flaws and make me future proof if some new file output type come into picture.

I have zeroed in to Factory and Strategy Patterns but unable to decide between the two.

Kindly suggest which one to pick in the situation explained above and why? Also if any other Design Pattern can also be implemented over here?

3
  • In this case, I'd use a Factory to return the appropriate Strategy pattern.
    – dbugger
    Dec 15, 2016 at 12:33
  • But don't you think it will be a over kill for this situation? I mean any one can solve my purpose. Please explain why you are saying that we need both? Dec 15, 2016 at 13:24
  • Why would it be overkill? I need a strategy to implement the appropriate output, I use a factory to get the proper strategy. Most systems use a combination of design patterns to solve separate problems, and this is a standard pairing. How else would you determine which strategy to use? My guess is it will end up looking a lot like a factory pattern, so why not use it explicitly?
    – dbugger
    Dec 15, 2016 at 13:36

1 Answer 1

2

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.

  1. Configure the type of possible file type/extension as a property
  2. The implementation classes for the provider contain the code for any file type conversions as per the format based on conversion and modification strategy.
  3. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.