이하와 같은 수준의 디자인을 가지고있다. 그래서 당신은 같은 인스턴스가 있다고 가정 할 수 있습니다 :
List<MyItem> itemsList = listGetter.getList();
listGetter.getList()
이 어디 당신이 무엇을 당신이 정말로 포함해야 목록을 얻기에 대해 알아 떠난다 코드입니다.
나는 각 값의 할당은이 목록을 업데이트하는 이상한 방법은 자신의 큰 논리를
있어 가지고 있기 때문에 다양한 클래스 라는 Enricher에서 이러한 클래스에 값을 할당/풍부하고있다. Enricher
클래스는 "필요"하지 않습니다. 거의 항상 다른 방법으로 수행 할 수 있습니다. 속성을 업데이트 할 때 "큰 논리"(왜이 문제를 정확히 해결할 수 있을지 모르겠다면 덜한 모호한 주석을 제공하지 않는 이유는 무엇입니까?) 다음 작업이 발생할 수있는 클래스로 그룹화 된 속성이 필요할 수 있습니다.
class MyItem {
int widgetVolume { get; set { MadLogic.doMadLogic(); } } // throws TooMadForYouBroException
int widgetDepth { get; set; }
int widgetWidth { get; set; }
int widgetHeight { get; set; }
int wodgetVolume { get; set; }
int wodgetDepth { get; set; }
int wodgetWidth { get; set; }
int wodgetHeight { get; set; }
}
는
class MyItem {
Widget widget { get; set; }
Wodget wodget { get; set; }
}
class Widget {
int widgetVolume { get; set; }
int widgetDepth { get; set; }
int widgetWidth { get; set; }
int widgetHeight { get; set; }
// Mad logic occuring here
void UpdateHeight(int height) {
this.widgetHeight = height;
this.widgetVolumne = this.widgetHeight * this.widgetLength * this.widgetDepth;
}
class Wodget {
// Logic could also be in these as setter methods.
int wodgetVolume { get; set; }
int wodgetDepth { get; set; }
int wodgetWidth { get; set; }
int wodgetHeight { get; set; }
void UpdateHeight(int height) {
// Mad logic occuring here
this.wodgetHeight = height;
// Store some "intermediate" stuff, totally made up psuedo-code
// I'm storing the height before the update, as an example
Database db = SomeDbDriver.Connect("db-connection-string");
db.SaveToTable("some-table", this.wodgetHeight);
// Property updated, as well as updated side effects for other properties.
this.wodgetVolumne = this.wodgetHeight * this.wodgetLength * this.wodgetDepth;
}
}
이 훨씬 더 관리 대상이됩니다! MyItem이 이제는 두 개의 속성 만 참조해야한다는 사실에 주목하십시오. 미친 미친 논리가 단일하고 관리 할 수없는 수천 줄 길이의 클래스 대신 자신의 클래스에 깔끔하게 자리 잡고 있습니다. 이러한 클래스를 별도의 파일로 분리 할 수도 있습니다. "WidgetHeightException"을 일으키는 특정 속성이 어디에 디버깅해야하는지 쉽게 알 수 있고 파일 내에 하나의 클래스가있는 것이 얼마나 쉬운 지 상상해보십시오 Widget.cs
!
컬렉션에 상단에 선언 된 itemsList
인 경우 System.Linq
을 사용할 수 있습니다. .Where()
를 사용하여 모든 영광스러운 방법은, 어쩌면이 같은 것을 사용하여 모든 목록의 속성을 업데이트 :
itemsList.Select(c => {
c.PropertyToSet = value;
return c;
}).ToList();
그러나, 그것은 정말 당신이 "목록을 업데이트"가 무엇을 의미하는지에 따라 달라집니다. 목록은 무엇입니까? 어떻게 선언됩니까? 저장 위치는 어디입니까? Enricher
개체가 클래스에 속성이 전혀 없을 때 어떻게 "중간 값 저장"할 수 있습니까?
다른 모든 하위 클래스에서 더 잘 그룹화 할 수 있다는 의미는 아닙니다. 또한 당신은 무엇을하려고하는지 의문입니다. –
저를 믿으십시오, 당신의 목표는 아마 저 많은 재산을 가진 그 크기 일 필요가없는 다! 클래스의 속성을 다른 클래스로 나눈 다음 해당 객체를 속성 유형으로 생각한 적이 있습니까? 'class RidiculousPropsClass (int mileage, int litresFuel, int ...)와 같은 것입니다.}'class RidiculousPropsClass {CarProperties properties} ... 클래스 CarProperties {int mileage, int litresFuel, int ...}' –
Observer 디자인 패턴을 살펴 보겠습니다. 지정된 오브젝트가 갱신됩니다. 귀하의 질문은 약간 혼란 스럽습니다. – meJustAndrew