는 이러한 코드 있다고 가정 :"기본 클래스 PARAMS는 항상 사용되지 않습니다"코드 냄새가
public Base
{
abstract void Register();
}
public Registrator1: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
public Registrator2: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
그러나 당신은 실제로 몇 가지 매개 변수에 따라 레지스터는 Registrator3를 작성 요구하는 새로운 비즈니스 규칙을 받고 당신은 변경할 때
public Base
{
abstract void Register(externalParam);
}
public Registrator1: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate theUI
}
}
public Registrator2: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate the UI
}
}
public Registrator3: Base
{
override void Register(externalParam)
{
//uses a DDD - service passed in the params to populate the UI
}
}
그러나 Registrator1과 Registrator2는 해당 param을 필요로하지 않으며 코드가 smelly가됩니다. 이 코드를 다시 작성하는 방법은 무엇입니까?
oop을 언급하면 메소드에 가상을 추가 할 수 있습니다. 이것은 코드 냄새가 "현재 같은 이름의 새 메소드"인 것처럼 보입니다. –
@Valentin Kuzub, done –
여전히 문제가 있습니다. 처음 메서드에는 params가 없습니다. 즉, 호출 된 객체의 상태를 조작하는 것입니다. 그 Register가 실제로하고있는 것이 명확하지 않아, 결함이나 더 나은 방법을 생각하기가 어렵습니다. externalParam을 추가하면 전체 그림이 완전히 바뀝니다. 등록자는 이제 매개 변수를 등록하거나 그 매개 변수를 기반으로 다른 매개 변수를 등록하여 상태가 충분하지 않습니다. 어쨌든 나는 좋은 조언을 원한다면 질문을 확장하고 예를 들어 등록 함수에 대한 의사 코드를 보여줄 수 있다고 생각한다. –