반복되는 코드 조각이 있습니다. 이것은 DRY principe와 모순됩니다. 그러나 이것을 일반적인 방법으로 대체하는 방법을 모르겠습니다.
class Foo
{
public bool isFirstAttribHasRightValue;
public bool isSecondAttribHasRightValue;
private readonly T1 _firstAttrib;
private readonly T2 _secondAttrib;
public HashSet<T1> relatedToFirstAttrib;
public HashSet<T2> relatedToSecondAttrib;
...
public C()
{ ... }
public T1 GetFirstAttrib(T3 somevalue)
{
return (somevalue != othervalue) || isFirstAttribHasRightValue ? _firstAttrib : default(T1);
}
public T2 GetSecondAttrib(T3 somevalue)
{
return (somevalue != othervalue) || isSecondAttribHasRightValue ? _secondAttrib : default(T2);
}
public ClearRelatedToFirst()
{
isFirstAttribHasRightValue = true;
relatedToFirstAttrib.Clear();
}
public ClearRelatedToSecond()
{
isSecondAttribHasRightValue = true;
relatedToSecondAttrib.Clear();
}
...
}
내가 좋아하는 중복 방법을 대체하기 위해 같은 ClearRelatedToFirst()
과 ClearRelatedToSecond()
에 ClearRelatedToAttrib<TYPE>()
. 그리고 그 generick 방법 안에 내가 어떤 선택하는 방법을 모르는 bool
세트거나 -variable 내가 필요 hashset
내가 삭제해야합니다. 다른 복제 방법과 동일합니다. 이 코드를 어떻게 리팩토링 할 수 있는지 보여줄 수 있습니까? 감사.
아래 코드를 참조하십시오.
class Attribute<T>
{
public bool isRightValue;
public HashSet<T> relatedHashSet;
private T _value;
public T GetValue(T3 somevalue)
{
return (somevalue != othervalue) || isRightValue ? _value : default(T);
}
public Clear()
{
isRightValue = true;
relatedHashSet.Clear();
}
}
class Foo
{
public Attribute<T1> firstAttribute;
public Attribute<T2> secondAttribute;
...
public Foo()
{ ... }
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다