简体   繁体   English

结构Java类(最佳实践)

[英]Structure java classes (best practice)

I have written some Java classes to import the content of Excel file. 我已经编写了一些Java类来导入Excel文件的内容。

The content of the excel has mainly the following data types: static, dynamic and static dynamic together. Excel的内容主要具有以下数据类型:静态,动态和静态动态结合在一起。

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. 现在的问题是我拥有同时具有动态和静态内容的excel。 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? 任何想法有什么办法可以解决此类问题?

使用ExcelImporter接口, AbstractExcelImporter获取共享代码和所需的三个实现。

You will need one abstract class with shared implementation. 您将需要一个具有共享实现的抽象类。 There is no need for 3 abstract classes. 不需要3个抽象类。 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 
   }
}

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. 反过来,可以使用属性和方法来增强ImportType枚举,该属性和方法可以将Excel数据导入到应用程序中。

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{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM