는이 코드가 .NET 일반 함께 작업 : 다음는 정적 및
public class EntityMapper<T> where T : IMappingStrategy, new()
{
private static T currentStrategy;
public static T CurrentStrategy
{
get
{
if (currentStrategy == null)
currentStrategy = new T();
return currentStrategy;
}
}
}
:
public static void Main()
{
EntityMapper<ServerMappingStrategy>.CurrentStrategy.ToString();
EntityMapper<ClientMappingStrategy>.CurrentStrategy.ToString();
EntityMapper<ServerMappingStrategy>.CurrentStrategy.ToString();
}
글쎄, 질문은 :
내가 디버깅있을 때 내가 볼 수있는 이유 ServerBussinessMappingStrategy의 생성자는 한 번만 호출됩니다.
이 작업은 훌륭하지만, 필자는 항상 EntityMapper가 필요한 올바른 인스턴스를 반환하고 ServerMappingStrategy 클래스를 한 번만 인스턴스화합니다.
감사합니다.
PD : 죄송합니다 내 영어 jeje가)
동기 문제는 선언에서 초기화하는 것이 가장 좋은 방법입니다. Jon Skeet은 여기에 더 자세히 설명되어 있습니다. http://www.yoda.arachsys.com/csharp/singleton.html –