나는 커다란 수업을 가지고 있는데, 'Country'라고 부르 자. 목록 상태, 연령 등과 같은 모든 종류의 속성을 가지고 있습니다.이 디자인 패턴의 이름은 무엇입니까? (만약 그렇다면)
실생활에서 저는 사용자가 클라이언트가 제공하는 서비스에 대한 구독을 등록하는 ASP.NET 응용 프로그램을 작성하고 있습니다. 이 애플리케이션 양식에는 많은 속성이 필요하며 데이터 모델을 대표하는 주요 클래스 중 하나가 너무 부풀어 오르기 때문에 컴포지션을 통해 작은 클래스로 나누기를 원하지만 상위 클래스에 연결된 하위 클래스에는 여전히 속성이 있습니다. 수업.
이 예제에서는 일을 단순하게 유지하고 자동차 예제를 참조하십시오. 설정의 빠른 샘플을 만들어 지금
public static class WorldDatabase
{
public static List<Country> Countries {get;set;}
}
class Country
{
public int Age {get;set;}
public string Name{get;set;}
public List<State> States{get;set;}
}
class State
{
public string CountryName{get;set;}
public string Capitol{get;set;}
public List<string> Cities{get;set;}
}
: : 그래서 우리는 다음과 같습니다 클래스라는 나라가
var states = new List<States>();
states.Add(new State(){CountryName="United States",
StateName="NJ",
Capitol="Trenton"});
WorldDatabase.Countries.Add(new Country{Age=237,
Name="United States",
States=states});
WorldDatabase.Countries[0].Name="US";
//assert obviously fails because the names are not linked
Assert.IsEqual(WorldDatabase.Countries[0].Name == states[0].CountryName);
그래서 내가 해결하기 위해 노력하고있어 문제 : 무엇이 가장 좋은 방법 두 속성을 연결 하시겠습니까? 내가 생각해 낸 방법은 부모 (국가)의 인스턴스를 State 클래스에 주입하는 것이다. 그러나 나는 부모가 부모가되어서는 안될 때 부모가 변화를 겪는 것에 대해 우려하고 있습니다. 또한 내가 알지 못하는 코드를 적게 사용하는 방법이있는 것처럼 보입니다. 다음은 내가 생각해 낸 두 가지 방법입니다.
//does a one-time 'binding'
class State
{
public void BindFrom(Country country)
{
CountryName=country.Name;
}
public string CountryName{get;set;}
public string Capitol{get;set;}
public List<string> Cities{get;set;}
}
//tracks the parent forever
class State
{
private readonly Country _parent;
public State(Country parent)
{
_parent = parent;
}
public string CountryName
{
get
{
return _parent.Name;
}
}
public string Capitol{get;set;}
public List<string> Cities{get;set;}
}
이 패턴의 이름은 무엇입니까 (있는 경우)? 나는 그것에 대해 더 많이 읽고 싶다 .. 그리고 대안이 있는가?
capitAl, 국회 의사당은 건물입니다 –
국가를 편집 할 수 없도록 국가를 제한하려면 2 번째 옵션을 유지하고 필요한 게터 만 제공하는 인터페이스를 추가하십시오 –