를 통해 클래스를 상속하는 모든 싱글을 받기 나는 다음과 같은 시나리오가 :반사
class Cow : Animal
{
public int animalID;
private static Cow instance;
public static Cow Instance
{
get
{
if (instance == null) instance = new Cow();
return instance;
}
}
private Cow() { }
}
Cow
이 Animal
에서 상속 보통 싱글입니다. 내가 필요로하는 것 : 으로부터 상속받은 모든 싱글 톤이있는 Dictionary<int, Animal>
. a)리스트는 이미 존재하는 모든 싱글 톤으로 이미 채워져있다. (b) 이미 존재하지 않는 내 사전 항목을 추가하는 메소드. 인스턴스화.
public class Cow : Animal { ... }
public class Goat : Animal { ... }
public class Zebra : Animal { ... }
public static class AnimalManagement
{
static Dictionary<int, Animal> zoo = new Dictionary<int, Animal>();
static void FillDictionary();
static Animal GetAnimalID(int animalID);
}
public Main()
{
var a1 = Cow.Instance;
var a2 = Goat.Instance;
AnimalManagement.FillDictionary();
// Now, zoo.Count() == 2
// Suppose I seeking for Zebra, with animalID == 5:
Animal zebra = AnimalManagement.GetAnimalID(5);
// Thus, zoo.Count() == 3 and zeebra singleton was
// instantiated and added to internal dic of AnimalManagement.
}
그래서 난 반사에 의해 실행 시간에 사전을 채우려 : 구현 클래스 암소를 들어
는 염소와 얼룩말은 그 동작을합니다. 내 친구가 될 수 있습니까?미리 감사드립니다. 기본적으로
왜 클래스 유형별로 싱글 톤을 원하십니까/필요합니까? –
불행히도 우리가 작업하고있는 클래스 모델이 있습니다. 현재로서는 변경할 수 없습니다. –
이 사전을 기본 클래스'Animal'에 추가하고, 파생 클래스에서 가져온 싱글 톤을 생성 할 때이 사전을 동기화 한 다음 노출 할 수 있습니까? –