構成、依存性注入などを必要とするクラスがあるとしましょう。
public class MyClass {
private String someConfig;
private SomeMutableClass anotherConfig;
MyClass() {
// impractical to set everything in ctor
// otherwise I'd declare someConfig final and
// not worry about MT safety.
}
void setConfig(cfg) {
this.someConfig = cfg;
}
void anotherConfig(cfg) {
this.anotherConfig = cfg;
}
...
// below is code that uses the config set before, possibly by
// multiple threads.
}
これは不自然な例ですが、ctorですべての構成を簡単に実行できない場合はどうなりますか?構成が実行の早い段階で行われ、変更されないとします。厳密に言えば、メモリモデルのために、someConfigへのすべての参照を同期する必要があります。この要件を実際に緩和することはできますか?