倭マン's BLOG

くだらない日々の日記書いてます。 たまにプログラミング関連の記事書いてます。 書いてます。

SimulationComponent インターフェース

前回、Simulator を構成するインターフェース群をリファクタリングしましたが、そこでは概ね「事前処理」「事後処理」に関連するメソッドを殆ど削除しました。 代わりに「SimulationComponent」インターフェースを定義し、その内部に定義したアノテーション「@Prepare」「@Dispose」を使用します。

SimulationComponent インターフェース


SimulationComponent 自体には実装すべきメソッドは定義してありません。 定義してあるのは

です。 JoinPoint に関しては次回以降に。

public interface SimulationComponent {

    enum JoinPoint {
        SIMULATE, ITERATE, EVOLVE
    }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @interface Prepare {
        JoinPoint value() default JoinPoint.SIMULATE;
    }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @interface Dispose {
        JoinPoint value() default JoinPoint.SIMULATE;
    }
}

@Prepare, @Dispose の使用例


@Prepare、@Dispose を用いて事前処理、事後処理を指定するには JUnit4.0 のようにします*1

public class SampleFileOutputter{

    ...

    private Writer writer;

    @Prepare
    public void prepareWriter(){
        this.writer = new FileWriter(getFileName());
    }

    @Dispose
    public void disposeWriter(){
        this.writer.flush();
        this.writer.close();
    }
}

JavaからRubyへ ―マネージャのための実践移行ガイド

JavaからRubyへ ―マネージャのための実践移行ガイド

*1:例外処理はとりあえずおいておきましょう。