前回、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(); } }
- 作者: Bruce A. Tate,角谷信太郎
- 出版社/メーカー: オライリー・ジャパン
- 発売日: 2007/04/21
- メディア: 単行本(ソフトカバー)
- 購入: 3人 クリック: 49回
- この商品を含むブログ (119件) を見る
*1:例外処理はとりあえずおいておきましょう。