Java 如何允许用户在FilenameFilter中编写自己的预定义方法实现,如accept()
Java 如何允许用户在FilenameFilter中编写自己的预定义方法实现,如accept(),java,design-patterns,itext,Java,Design Patterns,Itext,FilenameFilter有一个方法,我可以实现它来告诉系统我要如何过滤文件。我想实现类似的东西。以下是我的设想: 我正在编写一个通用API,它运行在添加到pdf文档的基础上。我有一个通用的自定义条形码 public class MyCustomBarcode{ /** * This variable holds the type of barcode * com.itextpdf.text.pdf.Barcode */ private Barcode barcode; /** *
FilenameFilter
有一个方法,我可以实现它来告诉系统我要如何过滤文件。我想实现类似的东西。以下是我的设想:
我正在编写一个通用API,它运行在添加到pdf文档的基础上。我有一个通用的自定义条形码
public class MyCustomBarcode{
/**
* This variable holds the type of barcode
* com.itextpdf.text.pdf.Barcode
*/
private Barcode barcode;
/**
* The X position of the barcode. (0, 0) is at the bottom left
*/
private float x;
/**
* The Y position of the barcode. (0, 0) is at the bottom left
*/
private float y;
/**
*
*/
private int rotation;
...
}
因此,当用户使用此API时,他们只需将列表
传递给my API中的方法,然后API将在每个pdf页面上插入条形码。问题是每个条形码都有不同的格式。例如,BarcodeInter25可能会使用类似于000001,000002…
的代码,而Barcode39可能会使用其他代码。所以我想让用户自己编写实现如何生成条形码的值。像这样的
MyCustomBarcode barcode = new MyCustomBarcode(x, y, z){
public String getSeqNum(int i){
//The user own implementation of how they want integer i to look like.
//E.g if i==1, I might return 000001
}
);
由于我允许用户拥有多种类型的条形码,我希望允许他们为他们拥有的每个条形码编写自己的实现。在父类中,使方法
抽象,然后让每个子类提供自己的具体实现
例子
父类
子类
我刚刚更新了我的问题,补充了一些细节。你能再给我一点信息吗。也许是一些伪代码,或者链接,或者我能看得更清楚一些的东西?@HarryPham,我举了一个例子。
public abstract class Barcode{
.
.
.
public abstract String getSeqNum(int i);
}
public final class FooBarcode extends Barcode{
.
.
.
public final String getSeqNum(int i){
// provide own implementation
}
}