public interface ISomeDependency
{
void Calculate(Person person);
}
public class SomeDependency : ISomeDependency
{
void ISomeDependency.Calculate(Person person)
{
person.Age = 30;
}
}
public class Person
{
private static ISomeDependency _someDependency;
public DateTime BirthDate { get; set; }
public int Age { get; set; }
public Person(ISomeDependency someDependency)
{
_someDependency = someDependency;
}
public void CalculateAge()
{
_someDependency.Calculate(this);
}
}
public class Client
{
public Client()
{
Person p = new Person(new SomeDependency());
p.BirthDate = DateTime.Now.AddYears(-30);
p.CalculateAge();
}
}
종속성이 정적인지 여부는 중요한 이유는 무엇입니까? 일반적으로 중요합니까?개인 정적 종속성과 개인 종속성
모든 'Person' 개체가 동일한 종속성 구현을 공유하는지 여부에 따라 다릅니다. 만약 그렇다면, 정적 일 것이지만, IMO,'Person'의 생성자에 넘겨서는 안됩니다. 왜냐하면 그것이 정적이 아니라는 것을 의미하기 때문입니다. –
정적 필드를 주입하는 것은 단일 인스턴스 (싱글 톤) 만 보장 할 수 없다면 큰 의미가 없습니다. – crush