Deciding between two class to be initialized to an interface at the time of initilization of a program; and using this settings throughout

In my program I need to create xls or xlsx files and the file type is opted using an option in a property file.

This is how we can switch xls and xlsx file type using java code

For xls:

Workbook workbook =  new XSSFWorkbook(); 

For xlsx:

Workbook workbook =  new HSSFWorkbook(); 

And what I do is, for creating every file i check the variable which contains the filetype option in propertie file.

if(variable which contains the filetype option in properties has xls ) {     Workbook workbook =  new XSSFWorkbook(); } else {     Workbook workbook =  new HSSFWorkbook(); } 

I am doing this for ever single files and our processes involves creation of lakhs of file. There are also conditions for creating filename for this files. Is there any way to fix a specific class(XSSFWorkbook|HSSFWorkbook) at the time of initialization of program and use it throughout the program instead of checking the condition for every file. I thought of singleton pattern but it is use of single object not class.


You want the Factory pattern. You inject a factory that can create Workbooks into the places that need them, and you inject either a XSSFWorkbookFactory or a HSSFWorkbookFactory depending on the property.

For this, you'll probably want to use the Dependency Injection pattern as well, to get the factory to where it's needed.

Category: java Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.162 (s). 12 q(s)