SlideShare a Scribd company logo
Refactoring:
Improving the Design
of Existing Code
VLAB – TEAM IT 01/2018
Cửa hàng
cho thuê video
1
Cửa hàng cho thuê Video
• Loại phim
◦ Regular
◦ Children
◦ New release
• Vấn đề
◦ Tính chi phí thuê video?
◦ Tính điểm khách hàng thân thiết?
◦ Mỗi lần thuê, cộng thêm 1 điểm
◦ Nếu thuê phim thuộc loại NEW RELEASE và thuê lâu hơn 1 ngày sẽ cộng thêm 1 điểm
2
Yêu cầu
Tính chi phí cho mỗi lần thuê?
◦ REGULAR, cộng 2. Nếu thuê lâu hơn 2 ngày, (số ngày – 2) nhân 1.5.
◦ NEW RELEASE, số ngày nhân 3.
◦ CHILDRENS, cộng 1.5. Nếu thuê lâu hơn 3 ngày, (số ngày thuê - 3) nhân 1.5.
Tính điểm mỗi lần thuê?
◦ Mỗi lần thuê, cộng thêm 1 điểm
◦ Nếu thuê phim thuộc loại NEW RELEASE và thuê lâu hơn 1 ngày sẽ cộng thêm 1 điểm
3
Cửa hàng cho thuê video
Movie Rental Customer
4
Hiện trạng
Hình 1. Hiện trạng trước khi refactor
5
Tips
• Before you start refactoring, check that you have a solid suite of tests. These tests must be self-
checking.
• Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the
bug.
• Any fool can write code that a computer can understand. Good programmers write code that
humans can understand.
6
Refactoring - #1
Hình 2. Áp dụng Extract Method và Move Method với hàm getCharge
7
Refactoring - #1
Hình 2. Sequence Diagram sau khi tái cấu trúc hàm getCharge
8
Refactoring - #2
Hình 2. Áp dụng Extract Method và Move Method hàm getFrequentRenterPoint
9
Refactoring - #2
Hình 2. Sequence Diagram sau khi tái cấu trúc hàm getFrequentRenterPoints
10
Refactoring - #3
Hình 2. Áp dụng Replace Temp with Query
11
Refactoring - #3
Hình 2. Sequence Diagram sau khi tái cấu trúc
12
Refactoring - #4
Hình 2. Áp dụng Move Method
13
Refactoring - #5
Hình 2. Áp dụng tính đa hình (Polymorphism)
14
Refactoring - #5
Hình 2. State Pattern Hình 2. Strategy Pattern
VS
“Allow an object to alter its behavior when its internal
state changes.The object will appear to change its
class.” - GoF
“Define a family of algorithms, encapsulate each
one, and make theminterchangeable. Strategy lets
the algorithm vary independently fromclients that
use it.” - GoF
15
Refactoring - #5
Hình 2. Áp dụng State Pattern
Movie sẽ thay đổi cách tính chi phí
khi PriceCode thay đổi
16
Refactoring - #5
Hình 2. Sequence Diagram sau khi áp dụng State Pattern
17
Refactoring - #5
Hình 2. Class Diagram sau khi áp dụng State Pattern
18
Các kỹ thuật
• Extract Method
• Move Method
• Replace Temp with Query
• Replace Type code with State/Stategy
• Replace Conditional with Polymorphism
• Self Encapsulate Field
19
Tham khảo
• Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts. Refactoring: Improving
the Design of Existing Code.: Addison-Wesley, 1991.
• Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns: Elements of
Reusable Object Oriented Software.: Addison-Wesley, 1995.
20

More Related Content

Recently uploaded

BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
support03
 

Recently uploaded (7)

Khóa luận tốt nghiệp Công nghệ kỹ thuật hóa học Xây dựng phương pháp định lượ...
Khóa luận tốt nghiệp Công nghệ kỹ thuật hóa học Xây dựng phương pháp định lượ...Khóa luận tốt nghiệp Công nghệ kỹ thuật hóa học Xây dựng phương pháp định lượ...
Khóa luận tốt nghiệp Công nghệ kỹ thuật hóa học Xây dựng phương pháp định lượ...
 
Google E-E-A-T là gì? Yếu tố giúp Google đánh giá website của bạn
Google E-E-A-T là gì? Yếu tố giúp Google đánh giá website của bạnGoogle E-E-A-T là gì? Yếu tố giúp Google đánh giá website của bạn
Google E-E-A-T là gì? Yếu tố giúp Google đánh giá website của bạn
 
Khóa luận tốt nghiệp Công tác Quản trị Văn phòng tại Công ty than Khe Chàm
Khóa luận tốt nghiệp Công tác Quản trị Văn phòng tại Công ty than Khe ChàmKhóa luận tốt nghiệp Công tác Quản trị Văn phòng tại Công ty than Khe Chàm
Khóa luận tốt nghiệp Công tác Quản trị Văn phòng tại Công ty than Khe Chàm
 
Checklist SEO để tối ưu website mà bạn cần biết.pdf
Checklist SEO để tối ưu website mà bạn cần biết.pdfChecklist SEO để tối ưu website mà bạn cần biết.pdf
Checklist SEO để tối ưu website mà bạn cần biết.pdf
 
BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
BOSCH FBX-1000 Bộ chống hú Bosch FBX-1000
 
Khoá luận tốt nghiệp Công nghệ sinh học Nghiên cứu tuyển chọn chủng vi sinh v...
Khoá luận tốt nghiệp Công nghệ sinh học Nghiên cứu tuyển chọn chủng vi sinh v...Khoá luận tốt nghiệp Công nghệ sinh học Nghiên cứu tuyển chọn chủng vi sinh v...
Khoá luận tốt nghiệp Công nghệ sinh học Nghiên cứu tuyển chọn chủng vi sinh v...
 
Tín hiệu xếp hạng trên Google là những gì.pdf
Tín hiệu xếp hạng trên Google là những gì.pdfTín hiệu xếp hạng trên Google là những gì.pdf
Tín hiệu xếp hạng trên Google là những gì.pdf
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
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)
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
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
 

Refactoring - Improve the design of existing code

  • 1. Refactoring: Improving the Design of Existing Code VLAB – TEAM IT 01/2018
  • 3. Cửa hàng cho thuê Video • Loại phim ◦ Regular ◦ Children ◦ New release • Vấn đề ◦ Tính chi phí thuê video? ◦ Tính điểm khách hàng thân thiết? ◦ Mỗi lần thuê, cộng thêm 1 điểm ◦ Nếu thuê phim thuộc loại NEW RELEASE và thuê lâu hơn 1 ngày sẽ cộng thêm 1 điểm 2
  • 4. Yêu cầu Tính chi phí cho mỗi lần thuê? ◦ REGULAR, cộng 2. Nếu thuê lâu hơn 2 ngày, (số ngày – 2) nhân 1.5. ◦ NEW RELEASE, số ngày nhân 3. ◦ CHILDRENS, cộng 1.5. Nếu thuê lâu hơn 3 ngày, (số ngày thuê - 3) nhân 1.5. Tính điểm mỗi lần thuê? ◦ Mỗi lần thuê, cộng thêm 1 điểm ◦ Nếu thuê phim thuộc loại NEW RELEASE và thuê lâu hơn 1 ngày sẽ cộng thêm 1 điểm 3
  • 5. Cửa hàng cho thuê video Movie Rental Customer 4
  • 6. Hiện trạng Hình 1. Hiện trạng trước khi refactor 5
  • 7. Tips • Before you start refactoring, check that you have a solid suite of tests. These tests must be self- checking. • Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug. • Any fool can write code that a computer can understand. Good programmers write code that humans can understand. 6
  • 8. Refactoring - #1 Hình 2. Áp dụng Extract Method và Move Method với hàm getCharge 7
  • 9. Refactoring - #1 Hình 2. Sequence Diagram sau khi tái cấu trúc hàm getCharge 8
  • 10. Refactoring - #2 Hình 2. Áp dụng Extract Method và Move Method hàm getFrequentRenterPoint 9
  • 11. Refactoring - #2 Hình 2. Sequence Diagram sau khi tái cấu trúc hàm getFrequentRenterPoints 10
  • 12. Refactoring - #3 Hình 2. Áp dụng Replace Temp with Query 11
  • 13. Refactoring - #3 Hình 2. Sequence Diagram sau khi tái cấu trúc 12
  • 14. Refactoring - #4 Hình 2. Áp dụng Move Method 13
  • 15. Refactoring - #5 Hình 2. Áp dụng tính đa hình (Polymorphism) 14
  • 16. Refactoring - #5 Hình 2. State Pattern Hình 2. Strategy Pattern VS “Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.” - GoF “Define a family of algorithms, encapsulate each one, and make theminterchangeable. Strategy lets the algorithm vary independently fromclients that use it.” - GoF 15
  • 17. Refactoring - #5 Hình 2. Áp dụng State Pattern Movie sẽ thay đổi cách tính chi phí khi PriceCode thay đổi 16
  • 18. Refactoring - #5 Hình 2. Sequence Diagram sau khi áp dụng State Pattern 17
  • 19. Refactoring - #5 Hình 2. Class Diagram sau khi áp dụng State Pattern 18
  • 20. Các kỹ thuật • Extract Method • Move Method • Replace Temp with Query • Replace Type code with State/Stategy • Replace Conditional with Polymorphism • Self Encapsulate Field 19
  • 21. Tham khảo • Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts. Refactoring: Improving the Design of Existing Code.: Addison-Wesley, 1991. • Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns: Elements of Reusable Object Oriented Software.: Addison-Wesley, 1995. 20