首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用模板模式设计通用流程

使用模板模式设计通用流程
EN

Stack Overflow用户
提问于 2011-12-19 21:20:09
回答 1查看 251关注 0票数 1

我有一个例行公事,我反复为许多项目做,我想概括它。我使用iText进行PDF操作。

假设我在一个文件夹中有2000个PDF,我需要将这些压缩在一起。假设限制是每拉链1000 PDF。因此,zip的名称将遵循以下规则:job name + job sequence。例如,第一个1000PDF的压缩名是XNKXMN + AA,第二个压缩名是XNKXMN + AB。在压缩这些PDF之前,我需要为每个PDF添加一些文本。文本看起来类似于这个job name + job sequence + pdf sequence。因此, first zip中的第一个PDF将包含以下文本:XNKXMN + AA + 000001,然后是XNKXMN + AA + 000002。这是我的尝试

首先,我有代表我的文本的抽象的clas GenericText

代码语言:javascript
复制
public abstract class GenericText {

    private float x;

    private float y;

    private float rotation;

    /**
     * Since the text that the user want to insert onto the Pdf might vary
     * from page to page, or from logical document to logical document, we allow
     * the user to write their own implementation of the text. To give the user enough
     * flexibility, we give them the reference to the physical page index, the logical page index. 
     * @param physcialPage The actual page number that the user current looking at
     * @param logicalPage A Pdf might contain multiples sub-documents, <code>logicalPage</code>
     * tell the user which logical sub-document the system currently looking at
     */
     public abstract String generateText(int physicalPage, int logicalPage);

     GenericText(float x, float y, float rotation){
           this.x = x;
           ...
     }

}

JobGenerator.java:我的通用API来做我上面描述的事情

代码语言:javascript
复制
public String generatePrintJob(List<File> pdfList, String outputPath,
        String printName, String seq, List<GenericText> textList, int maxSize)
for (int currentPdfDocument = 0; currentPdfDocument < pdfList.size(); currentPdfDocument++) {
    File pdf = pdfList.get(currentPdfDocument);
    if (currentPdfDocument % maxSize != 0) {
        if(textList != null && !textList.isEmpty()){
             for(GenericText gt : textList){
                 String text = gt.generateText(currentPdfDocument, currentPdfDocument)
                 //Add the text content to the PDF using PdfReader and PdfWriter
             }  
        } 
        ...
    }else{
          //Close the current output stream and zip output stream
          seq = Utils.getNextSeq(seq);
          jobPath = outputPath + File.separator + printName + File.separator + seq + ".zip"
          //Open new zip output stream with the new <code>jobPath</code>
    }
}
}

所以现在在我的主修课上,我会这样做

代码语言:javascript
复制
final String printName = printNameLookup.get(baseOutputName);
String jobSeq = config.getPrintJobSeq();
final String seq = jobSeq;
GenericText keyline = new GenericText(90, 640, 0){
    @Override
    public String generateText(int physicalPage, int logicalPage) {
         //if logicalPage = 1, Utils.right(String.valueOf(logicalPage), 6, '0') -> 000001
         return printName + seq + " " + Utils.right(String.valueOf(logicalPage), 6, '0');
    }
};
textList.add(keyline);
JobGenerator pjg = new JobGenerator();
pjg.generatePrintJob(...,..., printName, jobSeq, textList, 1000);

我在这个设计中遇到的问题是,即使我正确地将PDF归档到两个zip中,文本也没有得到正确的反映。打印和序列并没有相应的变化,它在2000年的PDF中保留了XNKXMN + AA,而在第一个1000中则保留了XNKXMN + AA,在后面的1000中则更改为XNKXMN + AB--我的设计似乎有缺陷,请帮助

编辑

在查看了toto2代码之后,我发现了我的问题。我创建GenericText时希望在pdf页面的任何地方添加文本,而不会影响这个过程的基本逻辑。但是,作业序列在定义上取决于逻辑,因为如果一个ZIP处理的PDF太多(> maxSize),它需要增加。我得重新考虑一下。

EN

回答 1

Stack Overflow用户

发布于 2011-12-19 22:22:41

创建匿名GenerateText时,您在重写的generateText方法中使用的final seq确实是最终的,并且始终保持在创建时给定的值。您在seq中的else中进行的更新什么也不做。

总的来说,您的代码看起来非常复杂,您可能应该后退一步,进行一些重要的重构。

编辑

相反,我会尝试一些不同的东西,没有模板方法模式:

代码语言:javascript
复制
int numberOfZipFiles = 
      (int) Math.ceil((double) pdfList.size() / maxSize);  

for (int iZip = 0; iZip < numberOfZipFiles; iZip++) {
  String batchSubName = generateBatchSubName(iZip); // gives AA, AB,...

  for (int iFile = 0; iFile < maxSize; iFile++) {
     int fileNumber = iZip * maxSize + iFile;
     if (fileNumber >= pdfList.size()) // can happen for last batch
        return;
     String text = jobName + batchSubName + iFile;
     ... add "text" to pdfList.get(fileNumber)
  }
}

但是,您也可能希望维护模板模式。在这种情况下,我将保留上面所写的for-循环,但是我会将生成方法更改为genericText.generateText(iZip, iFile),其中iZip =0给AA,iZip =1给AB,等等:

代码语言:javascript
复制
for (int iZip = 0; iZip < numberOfZipFiles; iZip++) {
  for (int iFile = 0; iFile < maxSize; iFile++) {
     int fileNumber = iZip * maxSize + iFile;
     if (fileNumber >= pdfList.size()) // can happen for last batch
        return;
     String text = genericText.generateText(iZip, iFile);
     ... add "text" to pdfList.get(fileNumber)
  }
}

也可以使用genericText.generateText(fileNumber)来分解AA000001中的fileNumber,等等,但这有点危险,因为maxSize将在两个不同的地方使用,而且可能会有类似的重复数据。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8567721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档