개발방법론/모델링/Refactoring

Replace Conditional with Polymorphism

아름프로 2003. 8. 2. 14:57

Replace Conditional with Polymorphism


You have a conditional that chooses different behavior depending on the type of an object.

Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.



        double getSpeed() {
                switch (_type) {
                        case EUROPEAN:
                                return getBaseSpeed();
                        case AFRICAN:
                                return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
                        case NORWEIGIAN_BLUE:
                                return (_isNailed) ? 0 : getBaseSpeed(_voltage);
                }
                throw new RuntimeException ("Should be unreachable");
        }




For more information see page 255 of Refactoring




***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)