Reputation: 2927
I have written some Java classes to import the content of Excel file.
The content of the excel has mainly the following data types: static, dynamic and static dynamic together.
The question is what is the best structure to write the importer classes. I need methods to import dynamic, static and dynamic-static content
My idea is the following:
//Class to import dynamic content
abstract class DynamicImporter{
void importDynamicExcel(){
}
//class to import static content
abstract class StaticImporter{
void importStaticExcel(){
}
Now the problem is that I have excel which have bouth dynamic and static content. It is not possible to do something like this
abstract class DynamicStaticImporter extends StaticImporter, StaticImporter{
}
Any Idea what could be alternative to solve such kind of problems?
Upvotes: 0
Views: 165
Reputation: 187
You can't inherit of two disctinct classes but you can implements as many interface as you want. You can do something like that :
interface DynamicImporter{
void importDynamicExcel(){
}
}
interface StaticImporter{
void importStaticExcel(){
}
}
And then, your class becomes :
abstract class DynamicStaticImporter implements StaticImporter, StaticImporter{
}
If you need some common code, you can also have
abstract class AbstractImporter {
someMethod() {
}
}
In that case, your class will become :
abstract class DynamicStaticImporter extends AbstractImporter implements StaticImporter, StaticImporter{
}
Upvotes: 0
Reputation: 727057
An alternative to implementing dispatch on multiple inheritance is to flatten the hierarchy into a single class, and give it a single method that takes a descriptor of what kind of import to do (static, dynamic, etc.) as its parameter:
enum ImportType {
STATIC
, DYNAMIC
, STATIC_DYNAMIC
}
class Importer {
void importExcel(ImportType impType) {
...
}
}
In turn ImportType
enumeration could be enhanced with properties and methods directing the process of importing Excel data into your application.
Upvotes: 0
Reputation: 574
You will need one abstract class with shared implementation. There is no need for 3 abstract classes. Alternatively you can use builder pattern for composition
abstract class AbstractExcelImporter{
void importExcel(){
//implementation
}
}
//Class to import dynamic content
class DynamicImporter extends AbstractExcelImporter{
void importDynamicExcel(){
importExcel();
//type spesific implementation or overridde importExcel method
}
}
Upvotes: 0
Reputation: 838
Use an ExcelImporter
interface, an AbstractExcelImporter
for shared code and the three implementations you need.
Upvotes: 1