SlideShare a Scribd company logo
디자인 패턴 세미나
 템플릿 메소드
박형근
     phg98@naver.com
http://www.codereview.co.kr
디자인 패턴 ?
디자인 패턴 ?

  “설계 시 겪게 되는
일반적인 문제상황에 대한
 이미 알려진 해결책”
템플릿 메소드
(Template Method)
템플릿 메소드 (Template Method)




•템플릿 : 양식
템플릿 메소드 (Template Method)




•템플릿 : 양식
예제 : 커피와 홍차
템플릿 메소드
•   크게 보았을 때 같은 처리를 하는 두 메소드가
•   세부적인 처리만 다를 경우
•   큰 틀(템플릿)을 담당하는 메소드를 만들고
•   세부적인 처리는 상속된 클래스에서 구현.
예제 : 기존에 구현된 커피 코드에
   홍차를 추가 해 봅시다.
커피 코드
Void main () {
   Coffee aCoffee;
   aCoffee.prepareRecipe();
}

public class Coffee {
   void prepareRecipe() {
         boilWater();
         brewCoffeeGrinds();
         pourInCup();
         addSugarAndMilk();
  }
}
기존 커피 코드에 홍차를 추가
Void main () {
   Coffee aCoffee;
   aCoffee.prepareRecipe();
}

public class Coffee {
   void prepareRecipe() {
         boilWater();
                               steepTeaBag();
         brewCoffeeGrinds();
         pourInCup();
         addSugarAndMilk();    addLemon();
  }
}
Case 1.함수 인자에 의한 처리
Void main () {
   Coffee aCoffee;
   aCoffee.prepareRecipe(isCoffee);
}

public class Coffee {
   void prepareRecipe(bool isCoffee) {
         boilWater();
         if (isCoffee) brewCoffeeGrinds();
         else          steepTeaBag();
         pourInCup();
         if (isCoffee) addSugarAndMilk();
         else          addLemon();
  }
}
SRP
Single Responsibility Principal
        단일책임원칙
Case 1. 함수 인자에 의한 처리
                          SRP 원칙에 위배
Void main () {
   Coffee aCoffee;
   aCoffee.prepareRecipe(isCoffee);
}

public class Coffee {
   void prepareRecipe(bool isCoffee) {
         boilWater();
         if (isCoffee) brewCoffeeGrinds();
         else          steepTeaBag();
         pourInCup();
         if (isCoffee) addSugarAndMilk();
         else          addLemon(); Cocoa 가
  }                                    추가된다면?
}
Case 2. Copy & Paste
Void main () {                 Void main () {
   Coffee aCoffee;                Tea aTea;
   aCoffee.prepareRecipe();       aTea.prepareRecipe();
}                              }

public class Coffee {          public class Tea {
   void prepareRecipe() {         void prepareRecipe() {
         boilWater();                   boilWater();
         brewCoffeeGrinds();            steepTeaBag();
         pourInCup();                   pourInCup();
         addSugarAndMilk();             addLemon();
  }                              }
}                              }
DRY
(Do Not Repeat Yourself)
Case 2. Copy & Paste
           Case 2. Copy & Paste
                               DRY 원칙에 위배
Void main () {                   Void main () {
   Coffee aCoffee;                  Coffee aTea;
   aCoffee.prepareRecipe();         aTea.prepareRecipe();
}                                }

public class Coffee {     public class Tea {
   void prepareRecipe() {    void prepareRecipe() {
         boilWater();              boilWater();
         brewCoffeeGrinds();       steepTeaBag();
         pourInCup();              pourInCup();
         addSugarAndMilk();        addLemon();
  }                                prepareSpoon();
}                           }
                          }
             공통 부분의 추가/수정시 동기화 문제발생
템플릿 메소드
(Template Method)
템플릿 메소드
템플릿 메소드
Void main () {
                                     class Coffee : public Beverage {
   Beverage *aBev = new Coffee;
                                     Protected:
   aBev->prepareRecipe();
                                        void brew() { brewCoffee(); }
}
                                        void add()
                                        {addSugarAndMilk();}
class Beverage {
                                     }
Public:
  void prepareRecipe() {
                                     class Tea : public Beverage {
        boilWater();
                                     Protected:
             brew();
                                        void brew() { steepTeaBag();}
             pourInCup();
                                        void add() { addLemon(); }
             add();                  }
    }
        virtual void brew()   = 0;
        virtual void add()    = 0;
}
템플릿 메소드
Void main () {
                                     class Coffee : public Beverage {
   Beverage *aBev = new Coffee;
                                     Protected:
   aBev->prepareRecipe();
                                        void brew() { brewCoffee(); }
}
                                        void add()
                                        {addSugarAndMilk();}
class Beverage {
                                     }
Public:
  void prepareRecipe() {
                                     class Tea : public Beverage {
        boilWater();
                                     Protected:
             brew();
                                        void brew() { steepTeaBag();}
             pourInCup();
                                        void add() { addLemon(); }
             add();                  }
    }
        virtual void brew()   = 0;
        virtual void add()    = 0;
}
템플릿 메소드
템플릿 메소드
• 두 클래스의 부모 클래스를 만든다.
• 좋은 이름이 중요하다 !!
• 부모 클래스에 템플릿 메소드를 만든다.
• 세부적으로 달라져야 할 부분을 가상함수로 정의
  (함수 signature가 같아야 한다.)
• 자식 클래스에서 세부정의를 구현한다.

• Application에서는 부모 클래스만 사용하도록 변경
고려할 점
•   실제 코드에서는 훨씬 복잡하다.
•   Pure virtual vs Hook
•   세부구현의 함수 인자가 다르면?
•   상속 vs 조합
템플릿 메소드 : 요약
•   크게 보았을때 같은 처리를 하는 두 메소드
•   세부적인 처리만 다를 경우
•   큰 틀(템플릿)을 담당하는 메소드
•   세부적인 처리는 상속된 클래스에서 정의
토의
감사합니다 !

More Related Content

Featured

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Design Pattern Template Method

  • 1. 디자인 패턴 세미나 템플릿 메소드
  • 2. 박형근 phg98@naver.com http://www.codereview.co.kr
  • 4. 디자인 패턴 ? “설계 시 겪게 되는 일반적인 문제상황에 대한 이미 알려진 해결책”
  • 6. 템플릿 메소드 (Template Method) •템플릿 : 양식
  • 7. 템플릿 메소드 (Template Method) •템플릿 : 양식
  • 9. 템플릿 메소드 • 크게 보았을 때 같은 처리를 하는 두 메소드가 • 세부적인 처리만 다를 경우 • 큰 틀(템플릿)을 담당하는 메소드를 만들고 • 세부적인 처리는 상속된 클래스에서 구현.
  • 10. 예제 : 기존에 구현된 커피 코드에 홍차를 추가 해 봅시다.
  • 11. 커피 코드 Void main () { Coffee aCoffee; aCoffee.prepareRecipe(); } public class Coffee { void prepareRecipe() { boilWater(); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk(); } }
  • 12. 기존 커피 코드에 홍차를 추가 Void main () { Coffee aCoffee; aCoffee.prepareRecipe(); } public class Coffee { void prepareRecipe() { boilWater(); steepTeaBag(); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk(); addLemon(); } }
  • 13. Case 1.함수 인자에 의한 처리 Void main () { Coffee aCoffee; aCoffee.prepareRecipe(isCoffee); } public class Coffee { void prepareRecipe(bool isCoffee) { boilWater(); if (isCoffee) brewCoffeeGrinds(); else steepTeaBag(); pourInCup(); if (isCoffee) addSugarAndMilk(); else addLemon(); } }
  • 15. Case 1. 함수 인자에 의한 처리 SRP 원칙에 위배 Void main () { Coffee aCoffee; aCoffee.prepareRecipe(isCoffee); } public class Coffee { void prepareRecipe(bool isCoffee) { boilWater(); if (isCoffee) brewCoffeeGrinds(); else steepTeaBag(); pourInCup(); if (isCoffee) addSugarAndMilk(); else addLemon(); Cocoa 가 } 추가된다면? }
  • 16. Case 2. Copy & Paste Void main () { Void main () { Coffee aCoffee; Tea aTea; aCoffee.prepareRecipe(); aTea.prepareRecipe(); } } public class Coffee { public class Tea { void prepareRecipe() { void prepareRecipe() { boilWater(); boilWater(); brewCoffeeGrinds(); steepTeaBag(); pourInCup(); pourInCup(); addSugarAndMilk(); addLemon(); } } } }
  • 17. DRY (Do Not Repeat Yourself)
  • 18. Case 2. Copy & Paste Case 2. Copy & Paste DRY 원칙에 위배 Void main () { Void main () { Coffee aCoffee; Coffee aTea; aCoffee.prepareRecipe(); aTea.prepareRecipe(); } } public class Coffee { public class Tea { void prepareRecipe() { void prepareRecipe() { boilWater(); boilWater(); brewCoffeeGrinds(); steepTeaBag(); pourInCup(); pourInCup(); addSugarAndMilk(); addLemon(); } prepareSpoon(); } } } 공통 부분의 추가/수정시 동기화 문제발생
  • 21. 템플릿 메소드 Void main () { class Coffee : public Beverage { Beverage *aBev = new Coffee; Protected: aBev->prepareRecipe(); void brew() { brewCoffee(); } } void add() {addSugarAndMilk();} class Beverage { } Public: void prepareRecipe() { class Tea : public Beverage { boilWater(); Protected: brew(); void brew() { steepTeaBag();} pourInCup(); void add() { addLemon(); } add(); } } virtual void brew() = 0; virtual void add() = 0; }
  • 22. 템플릿 메소드 Void main () { class Coffee : public Beverage { Beverage *aBev = new Coffee; Protected: aBev->prepareRecipe(); void brew() { brewCoffee(); } } void add() {addSugarAndMilk();} class Beverage { } Public: void prepareRecipe() { class Tea : public Beverage { boilWater(); Protected: brew(); void brew() { steepTeaBag();} pourInCup(); void add() { addLemon(); } add(); } } virtual void brew() = 0; virtual void add() = 0; }
  • 24. 템플릿 메소드 • 두 클래스의 부모 클래스를 만든다. • 좋은 이름이 중요하다 !! • 부모 클래스에 템플릿 메소드를 만든다. • 세부적으로 달라져야 할 부분을 가상함수로 정의 (함수 signature가 같아야 한다.) • 자식 클래스에서 세부정의를 구현한다. • Application에서는 부모 클래스만 사용하도록 변경
  • 25. 고려할 점 • 실제 코드에서는 훨씬 복잡하다. • Pure virtual vs Hook • 세부구현의 함수 인자가 다르면? • 상속 vs 조합
  • 26. 템플릿 메소드 : 요약 • 크게 보았을때 같은 처리를 하는 두 메소드 • 세부적인 처리만 다를 경우 • 큰 틀(템플릿)을 담당하는 메소드 • 세부적인 처리는 상속된 클래스에서 정의