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
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
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
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