JFreeChartでのTemplateモードの適用


今日SpringのJDBCのパッケージを見ていて、その中でJDBCtemplateに言及したので、Templateモードを思い出しました.このモードは、以前見たことがありますが、ありません.そして、もう長い間前のことです.ほとんど忘れています.ネット上で収めて、Jdonの上でTemplateモードについての文章を探して、このモードをもう一度温めました.
Templateテンプレートパターン定義:操作中のアルゴリズムのスケルトンを定義し、いくつかのステップの実行をそのサブクラスに遅延する.
Javaの抽象クラスを使用する場合、Templateモードを使用することが多いため、Templateモードの使用は一般的である.しかも分かりやすく使いやすいです.
public abstract class Benchmark
{
  /**
  *                
  */
  public abstract void benchmark(); 

  /**
  *     benchmark  
  */
  public final long repeat (int count) {
    if (count <= 0)
      return 0;
    else {
      long startTime = System.currentTimeMillis();

    for (int i = 0; i < count; i++) 
      benchmark();

    long stopTime = System.currentTimeMillis();
    return stopTime - startTime;
  }
}
}



 
上記の例では、benchmark()操作を繰り返し実行することを望んでいますが、benchmark()の具体的な内容については説明せず、そのサブクラスに遅延して説明します.
 
public class MethodBenchmark extends Benchmark
{
  /**
  *     benchmark  
  */
  public void benchmark() {

    for (int i = 0; i < Integer.MAX_VALUE; i++){
      System.out.printtln("i="+i);    
    }
  }
}



 
これでTemplateモードは完了しましたが、簡単ではないでしょうか.
我々はrepeat法をテンプレート法と呼び,その中のbenchmark()実装をサブクラスMethodBenchmarkに遅延させて実装した.
使用方法を見てみましょう.
Benchmark operation = new MethodBenchmark();
long duration = operation.repeat(Integer.parseInt(args[0].trim()));
System.out.println("The operation took " + duration + " milliseconds");

JFreeChartを使用する場合、chartを生成するステップは一定であるが、データセットと生成されたchartオブジェクトは不確定であるため、Templateを使用してChartの作成プロセスを簡略化することができる.