public static void initialize(){ //レジストリの再初期化 soleInstance = new Registry(); }
public static void initializeStub(){ //サブクラスの代入 soleInstance = new RegistryStub(); }
public static PersonFinder personFinder(){ //自身のインスタンスのインスタンス変数を参照 return getInstance().personFinder; } } class RegistryStub extends Registry{ public RegistryStub(){ Soleinstance = new Sub(); } } class Sup{ //コードA }
}
class Registry {
protected PersonFinder personFinder = new PersonFinder();
private static Registry soleInstance = new Registry();
private static Registry getInstance() {
// 静的な自身のインスタンスを返す
return soleInstance;
}
public static void initialize() {
// レジストリの再初期化
soleInstance = new Registry();
}
public static void initializeStub() {
// サブクラスの代入
soleInstance = new RegistryStub();
}
public static PersonFinder personFinder() {
// 自身のインスタンスのインスタンス変数を参照
return getInstance().personFinder;
}
}
class RegistryStub extends Registry {
public RegistryStub() {
this.personFinder=new PersonFinderStub();
}
}
//レイヤースーパークラス
abstract class DomainFinder{}
class PersonFinder extends DomainFinder{
public void someOperation(){}
}
class PersonFinderStub extends PersonFinder{
public void someOperation(){}
}
}
class Registry {
//クラス変数にしてもいいのでは?
//これが、問題になっていたのでは?
protected static PersonFinder personFinder = new PersonFinder();
private static Registry soleInstance = new Registry();
private static Registry getInstance() {
// 静的な自身のインスタンスを返す
return soleInstance;
}
public static void initialize() {
// レジストリの再初期化
soleInstance = new Registry();
}
public static void initializeStub() {
// サブクラスの代入
soleInstance = new RegistryStub();
}
public static PersonFinder personFinder() {
// 自身のインスタンスのインスタンス変数を参照
return getInstance().personFinder;
}
}
class RegistryStub extends Registry {
public RegistryStub() {
this.personFinder=new PersonFinderStub();
}
}
abstract class DomainFinder{
}
class PersonFinder extends DomainFinder{
public void someOperation(){}
}
class PersonFinderStub extends PersonFinder{
public void someOperation(){}
}