Template Method1 1 E Gamma R Helm R

  • Slides: 39
Download presentation

差分を含む類似メソッド集約 • Template Methodパターン[1]を適用すれば差分を 含む類似メソッドを集約できる. – しかし,熟練した開発者でないと適用は困難である. [1] E. Gamma, R. Helm, R. Johnson,

差分を含む類似メソッド集約 • Template Methodパターン[1]を適用すれば差分を 含む類似メソッドを集約できる. – しかし,熟練した開発者でないと適用は困難である. [1] E. Gamma, R. Helm, R. Johnson, and J. M. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995. 2

Template Methodパターンの例 Abstract. Class #method 1() #method 2() +template. Method() Concrete. Class 1 #method

Template Methodパターンの例 Abstract. Class #method 1() #method 2() +template. Method() Concrete. Class 1 #method 1() #method 2() public … template. Method{ … this. method 1() … this. method 2() … } Concrete. Class 2 #method 1() #method 2() 共通の処理を 親クラスで定義 抽象メソッドのみを オーバーライドして 子クラスを追加 New. Class #method 1() #method 2() 子クラスごとに 処理の実装を行う 4

Template Methodの形成 • Template Methodパターンに基づくリファクタリング パターン • Template Methodの形成を,Fowlerの書籍[2]で紹 介されている例を用いて説明する. [2] M. Fowler. Refactoring:

Template Methodの形成 • Template Methodパターンに基づくリファクタリング パターン • Template Methodの形成を,Fowlerの書籍[2]で紹 介されている例を用いて説明する. [2] M. Fowler. Refactoring: Improving the Design of Existing Code. Addison Wesley, 1999. 5

Template Methodの形成の例 Site Residential. Site Lifeline. Site get. Billable. Amount() … double base =

Template Methodの形成の例 Site Residential. Site Lifeline. Site get. Billable. Amount() … double base = _units*_rate; double tax = base*Site. TAX_RATE; return base + tax; … double base = _units*_rate*0. 5; double tax = base*Site. TAX_RATE*0. 2; return base + tax; 6

Template Methodの形成の例 手順1: 固有の処理を求める 差分から固有の 処理を求める Residential. Site Lifeline. Site 差分を見つける get. Billable. Amount()

Template Methodの形成の例 手順1: 固有の処理を求める 差分から固有の 処理を求める Residential. Site Lifeline. Site 差分を見つける get. Billable. Amount() 固有の処理 … double base = _units*_rate; double tax = base*Site. TAX_RATE; return base + tax; … double base = _units*_rate*0. 5; double tax = base*Site. TAX_RATE*0. 2; return base + tax; 7

Template Methodの形成の例 手順2: 固有の処理を取り除く Site Residential. Site Lifeline. Site get. Billable. Amount() get. Base.

Template Methodの形成の例 手順2: 固有の処理を取り除く Site Residential. Site Lifeline. Site get. Billable. Amount() get. Base. Amount() get. Tax. Amount() メソッドとして抽出 … double base = get. Base. Amount(); double tax = get. Tax. Amount(); return base + tax; 8

Template Methodの形成の例 手順3: 類似メソッドを引き上げる Site … double base = get. Base. Amount(); double tax

Template Methodの形成の例 手順3: 類似メソッドを引き上げる Site … double base = get. Base. Amount(); double tax = get. Tax. Amount(); return base + tax; get. Billable. Amount() get. Base. Amount() get. Tax. Amount() 親クラスに引き上げる 親クラスに Residential. Site 抽象メソッドを定義 get. Base. Amount() get. Tax. Amount() Lifeline. Site get. Base. Amount() get. Tax. Amount() 9

メトリクスCOB(Cohesion Of Blocks) • 21

メトリクスCOB(Cohesion Of Blocks) • 21

メトリクスCOBの値が低い例 void method() { int v 1, v 2, v 3, v 4; BLOCK

メトリクスCOBの値が低い例 void method() { int v 1, v 2, v 3, v 4; BLOCK 1 { v 1 = v 1 + v 2; } BLOCK 2 { v 2 = v 1++; } BLOCK 3 { v 3 = v 3 * v 4; } BLOCK 4 { v 4 = v 3 + 1; } } v1 v 2 v 3 v 4 BLOCK 1 ✓ ✓ BLOCK 2 ✓ ✓ BLOCK 3 ✓ ✓ BLOCK 4 ✓ ✓ COB = 0. 5 22

メトリクスCOBの値が高い例 void method() { int v 1, v 2, v 3; BLOCK 1 {

メトリクスCOBの値が高い例 void method() { int v 1, v 2, v 3; BLOCK 1 { v 1 = v 1 + v 2; } BLOCK 2 { v 2 = v 1++; } BLOCK 3 { v 3 = v 3 * v 1; } BLOCK 4 { v 1 = v 3 + 1; } } v1 v 2 v 3 BLOCK 1 ✓ ✓ BLOCK 2 ✓ ✓ BLOCK 3 ✓ ✓ BLOCK 4 ✓ ✓ COB = 0. 66 23

実験対象 • Antプロジェクト (ファイル数: 789) – Arcクラス execute. Draw. Operation() ・・・ 35行 – Ellipseクラス

実験対象 • Antプロジェクト (ファイル数: 789) – Arcクラス execute. Draw. Operation() ・・・ 35行 – Ellipseクラス execute. Draw. Operation() ・・・ 32行 • Azureusプロジェクト (ファイル数: 2684) – MD 5クラス digest() ・・・ 34行 – SHA 1クラス digest() ・・・ 33行 • ANTLRプロジェクト (ファイル数: 209) – Cpp. Code. Generatorクラス gen. Error. Handler() ・・・ 37行 – Java. Code. Generatorクラス gen. Error. Handler() ・・・ 34行 26

33

33