분류 전체보기
-
리팩토링 : Replace Iteration with Recursion리팩토링 2013. 2. 8. 09:00
작자 : Dave Whipp 조건루프가 있는데 각 반복이 어떤걸 하는지 명확하지 않다면적용재귀호출하는 반복으로 대체하시오. 적용전 unsigned greatest_common_divisor (unsigned a, unsigned b) { while (a != b) { if (a > b) { a -= b; } else if (b > a) { b -= a; } } } 적용후 unsigned greatest_common_divisor (unsigned a, unsigned b) { if (a > b) { return greatest_common_divisor ( a-b, b ); } else if (b > a) { return greatest_common_divisor ( a, b-a ); } else // ..
-
행위패턴 : Chain of Responsibility 패턴디자인패턴 2013. 2. 7. 09:30
분류 : 행위패턴(Behavioral Patterns) 정의 :명령(command) 객체들과 프로세싱(processing) 객체들의 집합으로 구성된 디자인 패턴. 각각의 프로세싱객체들은 명령객체가 처리할수 있는 타입을 정의하는 로직을 포함. 용도 :좀 더 느슨한 결합을 가능하게 만듬. 소스 public abstract class Logger { public static int ERR = 3; public static int NOTICE = 5; public static int DEBUG = 7; protected int mask; //chain of responsibility의 다음 요소 protected Logger next; public void setNext(Logger log) { next = l..
-
구조패턴 : Proxy 패턴디자인패턴 2013. 2. 6. 10:00
분류 : 구조패턴(Structural Patterns) 정의 :일반적인 형태의 프록시는 중복하기에는 너무 비용이 비싸거나 중복자체가 불가능한 네트워크 연결, 메모리내부의 대형객체, 파일, 또는 다른 자원들에 대한 인터페이스이다. 용도 :어플리케이션의 메모리 자원을 절약하기위해 사용될 수 있음. 소스 public interface Image { public void displayImage(); } //System A public class RealImage implements Image { private String filename = null; /** * 생성자 * @param FILENAME */ public RealImage(final String FILENAME) { filename = FILENA..
-
리팩토링 : Replace Exception with Test리팩토링 2013. 2. 6. 09:00
조건호출자가 처음 확인하는 조건에서 예외를 던지고 있다면 적용해당 호출자가 테스트를 먼저하도록 변경하시오. 적용전 double getValueForPeriod (int periodNumber) { try { return _values[periodNumber]; } catch (ArrayIndexOutOfBoundsException e) { return 0; } } 적용후 double getValueForPeriod (int periodNumber) { if (periodNumber >= _values.length) return 0; return _values[periodNumber]; } 참조http://www.refactoring.com/catalog/replaceExceptionWithTest.html
-
구조패턴 : Flyweight 패턴디자인패턴 2013. 2. 5. 09:30
분류 : 구조패턴(Structural Patterns) 정의 :다른 유사한 객체와 가능한 많은 데이터를 공유함으로써 메모리 사용량을 최소화하는 객체를 flyweight라고 함. 용도 :메모리 사용량을 줄여서 효율성을 높임. 소스 //Flyweight 객체 인터페이스 public interface CoffeeOrder { public void serveCoffee(CoffeeOrderContext context); } //ConcreteFlyweight를 생성하는 ConcreteFlyweight 객체 public class CoffeeFlavor implements CoffeeOrder { private final String flavor; public CoffeeFlavor(String newFlavor..
-
리팩토링 : Replace Error Code with Exception리팩토링 2013. 2. 5. 09:00
조건메소드가 에러대신에 특정 코드를 반환한다면적용대신에 예외를 던지시오. 적용전 int withdraw(int amount) { if (amount > _balance) return -1; else { _balance -= amount; return 0; } } 적용후 void withdraw(int amount) throws BalanceException { if (amount > _balance) throw new BalanceException(); _balance -= amount; } 참조http://www.refactoring.com/catalog/replaceErrorCodeWithException.html