Annual Summary 2008 REFACTORING TO PATTERNS Patterns Refactoring

  • Slides: 37
Download presentation
아. 꿈. 사 Annual Summary 2008 REFACTORING TO PATTERNS 이동현

아. 꿈. 사 Annual Summary 2008 REFACTORING TO PATTERNS 이동현

Patterns Refactoring XP

Patterns Refactoring XP

Standing on the Shoulders of Giants

Standing on the Shoulders of Giants

이 책을 쓴 이유 Over-Engineering Under-Engineering The Pattern Panacea Test Driven Development and Continuous

이 책을 쓴 이유 Over-Engineering Under-Engineering The Pattern Panacea Test Driven Development and Continuous Refactoring and Patterns Evolutionary Design

TDD & Continuous Refactoring Ask Respond Refind Repeat

TDD & Continuous Refactoring Ask Respond Refind Repeat

Refactoring and Patterns 마틴의 리팩토링을 적용하면서 디자인 개선에 패턴이 도움이 되는걸 발견했다. 디자인 패턴만

Refactoring and Patterns 마틴의 리팩토링을 적용하면서 디자인 개선에 패턴이 도움이 되는걸 발견했다. 디자인 패턴만 공부하면 원래의 핵심인 동기에 대한 부분을 놓치기 쉽다. Intent 에 집중하게 된다. Applicability section 이라도 자세히 봤다면. . Our design patterns capture many of the structures that result from refactoring. . . Design patterns thus provide targets for your refactoring. [DP, 354] There is a natural relation between patterns and refactorings. Patterns are where you want to be; refactorings are ways to get there from somewhere else. [F, 107]

Catalog of Code Smells Duplicated Code Long Method Conditional Complexity Primitive Obsession Indecent Exposure

Catalog of Code Smells Duplicated Code Long Method Conditional Complexity Primitive Obsession Indecent Exposure Solution Sprawl Alternative Classes with Different Interfaces Lazy Class Large Class Switch Statements Combinational Explosion Oddball Solution

Alternative Classes with Different Interfaces Unify Interfaces with Adapter( 247 ). Combinatorial Explosion Replace

Alternative Classes with Different Interfaces Unify Interfaces with Adapter( 247 ). Combinatorial Explosion Replace Implicit Language with Interpreter(269) Conditional Complexity Replace Conditional Logic with Strategy(129) Move Embellishment to Decorator(144) Replace State-Altering Conditionals with State(166) Introduce Null Object(301) Duplicated Code Form Template Method(205) Introduce Polymorphic Creation with Factory Method(88) Chain Constructors(340) Replace One/Many Distinctions with Composite(224) Extract Composite(214) Unify Interface with Adapter(247) Introduce Null Object(301) Indecent Exposure Encapsulate Classes with Factory(80) Large Class Replace Conditional Dispatcher with Command(191) Replace State-Altering Conditionals with State(166) Replace Implicit Language with Interpreter(269) Lazy Class Inline Singleton(114) Long Method Compose Method(123) Move Accumulation to Collection Parameter(313) Replace Conditional Dispatcher with Command(191) Move Accumulation to Visitor(320) Replace Conditional Logic with Strategy(129) Oddball Solution Unify Interfaces with Adapter(247) Primitive Obsession Replace Type Code with Class(286) Replace Sate-Altering Conditionals with State(166) Replace Conditional Logic with Strategy(129) Replace Implicit Tree with Composite(178) Replace Implicit Language with Interpreter(269) Move Embellishment to Decorator(144) Encapsulate Composite with Builder(96) Solution Sprawl Move Creation Knowledge to Factory(68) Switch Statements Replace Conditional Dispatcher with Command(191) Move Accumulation to Visitor(320)

리팩터링 형식 States Pattern Name and Intent Gives an application example Discusses motivation Benefits

리팩터링 형식 States Pattern Name and Intent Gives an application example Discusses motivation Benefits and Liabilities Mechanics Specific things to do Presents detailed example

Replace Constructors with Creation Methods

Replace Constructors with Creation Methods

예제 public class Loan { public Load( double commitment, int risk. Rating, Date maturity)

예제 public class Loan { public Load( double commitment, int risk. Rating, Date maturity) { this(commitment, 0. 00, risk. Rating, maturity, null); } public Load( double commitment, int risk. Rating, Date maturity, Date expiry ) { this(commitment, 0. 00, risk. Rating, maturity, expiry ); } public Load( double commitment, double outstanding, int risk. Rating, Date maturity, Date expiry) { this(null, commitment, outstanding, risk. Rating, maturity, expiry ); } public Load( Capital. Strategy capital. Strategy, double commitment, int risk. Rating, Date maturity, Date expiry) { this( capital. Strategy, commitment, 0. 0, risk. Rating, maturity, expiry ); } public Load( Capital. Strategy capital. Strategy, double commitment, double outstanding, int risk. Rating, Date maturity, Date expiry ) { this. commitment = commitment; this. outstanding = outstanding; this. risk. Rating = risk. Rating; this. maturity = maturity; this. expiry = expiry; this. capital. Strategy = captial. Strategy; if(capital. Strategy == null ) { if( expiry == null ) this. capital. Strategy = new Capital. Strategy. Term. Loan(); else if( maturity == null ) this. capital. Strategy = new Capital. Strategy. Revolver(); else this. capital. Strategy = new Capital. Strategy. RCTL(); } } }

1. Load 생성자를 하나 고르고 이 생성자를 사용하는 클라이언트 코드 를 찾는다. public class

1. Load 생성자를 하나 고르고 이 생성자를 사용하는 클라이언트 코드 를 찾는다. public class Capital. Calculation. Tests. . . public void test. Term. Loan. No. Payments() {. . . Loan tem. Loadn = new Loan(commitment, risk. Rating, maturity); . . . }

2. Extract Method 리팩터링을 적용해 Create. Term. Loan 이라는 public 메서드를 만든다. public class

2. Extract Method 리팩터링을 적용해 Create. Term. Loan 이라는 public 메서드를 만든다. public class Capital. Calculation. Tests. . . public void test. Term. Loan. No. Payments() {. . . Loan tem. Loadn = new Loan(commitment, risk. Rating, maturity); . . . } public class Capital. Calculation. Tests. . . public void test. Term. Loan. No. Payments() { //. . . public Loan create. Term. Loan(double commitment, int risk. Rating, Date maturity ) Loan { tem. Loadn = create. Term. Loan(commitment, risk. Rating, maturity); //. . . return new Loan(commitment, risk. Rating, maturity); } } Extract Method

3. Move Method 리팩터링을 적용해 생성 메서드 create. Term. Loan()을 Loan 클래스로 옮긴다. public

3. Move Method 리팩터링을 적용해 생성 메서드 create. Term. Loan()을 Loan 클래스로 옮긴다. public class Capital. Calculation. Tests. . . public void test. Term. Loan. No. Payments() { //. . . Loan tem. Loadn = create. Term. Loan(commitment, risk. Rating, maturity); //. . . } public Loan create. Term. Loan(double commitment, int risk. Rating, Date maturity ) { return new Loan(commitment, risk. Rating, maturity); } Move Method public class Loan public static Loan create. Term. Loan(double commitment, int risk. Rating, Date maturity) { return new Loan(commitment, risk. Rating, maturity); }

4. 선택한 생성자를 사용하는 모든 클라이언트 코드를 수정한다. public class Capital. Calculation. Tests. .

4. 선택한 생성자를 사용하는 모든 클라이언트 코드를 수정한다. public class Capital. Calculation. Tests. . . public void test. Term. Loan. No. Payments(){ //. . . Loan tem. Loadn = Loan. create. Term. Loan(commitment, risk. Rating, maturity); //. . . } }

5. 이제 선택한 생성자를 사용하는 곳은 Create. Term. Loan 뿐이다. Inline method 를 적용해

5. 이제 선택한 생성자를 사용하는 곳은 Create. Term. Loan 뿐이다. Inline method 를 적용해 생성자 호출 부분을 제거한다. public class Loan public Loan(double commitment, int risk. Rating, Date maturity){ this(commitment, 0. 00, risk. Rating, maturity, null) } public static Loan create. Term. Loan(double commitment, int risk. Rating, Date maturity) { return new Loan(commitment, 0. 00, risk. Rating, maturity, null); }

묵시적 언어 10달러 이하의 상품을 찾아라. 흰색이 아니고 10달러 이하인 상품을 찾아라. 파란색이고 작으며

묵시적 언어 10달러 이하의 상품을 찾아라. 흰색이 아니고 10달러 이하인 상품을 찾아라. 파란색이고 작으며 20달러 이하인 상품을 찾아라. Product. Finder… public List by. Color(. . ); … public List below. Price. Avoiding. AColor(float price, Color color); public List by. Color. And. Below. Price(Color color, float price); public List by. Color. Size. And. Below. Price(…)

Interpreter below. Price. Avoiding. AColor… and Price < target price not Color == target

Interpreter below. Price. Avoiding. AColor… and Price < target price not Color == target color And. Spec spec = new And. Spec( new Below. Price. Spec(price), new Not. Spec( new Color. Spec(color) ) );

Q&A

Q&A