-
메소드 정리 : Replace Method with Method ObjectProgramming/리팩토링 2020. 9. 4. 16:51
참고자료
https://refactoring.guru/replace-method-with-method-object
Replace Method with Method Object
Tired of reading? No wonder, it takes 7 hours to read all of the text we have here. Try our interactive course on refactoring. It offers a less tedious approach to learning new stuff. Let's see...
refactoring.guru
정의
함수가 너무 길고 서로 분리하기 어려운 지역 변수의 얽힌 덩어리로 인해 분리 할 수 없을 때,
함수를 별도의 클래스로 선언하여 지역 변수가 클래스의 필드가 되도록 합니다.
그런 다음 함수를 동일한 클래스 내에서 여러 메서드로 분할 할 수 있습니다.
예시
class Order { // ... public double price() { double primaryBasePrice; double secondaryBasePrice; double tertiaryBasePrice; // Perform long computation. } }
리팩토링 예시
class Order { // ... public double price() { return new PriceCalculator(this).compute(); } } class PriceCalculator { private double primaryBasePrice; private double secondaryBasePrice; private double tertiaryBasePrice; public PriceCalculator(Order order) { // Copy relevant information from the // order object. } public double compute() { // Perform long computation. } }
반응형'Programming > 리팩토링' 카테고리의 다른 글
객체 간 기능 이동 : Extract Class, Inline Class (0) 2020.09.05 객체 간 기능 이동 : Move Method, Move Field (0) 2020.09.05 메소드 정리 : Remove Assignments to Parameters (0) 2020.09.04 메소드 정리 : Split Temporary Variable (0) 2020.09.04 메소드 정리 : Replace Temp with Query (0) 2020.09.04