2. ABOUT ME
Nguyễn Xuân Kiên
Techlead at Shippo.vn
Experienced with
ASP.NET
JAVA
PHP
Javascript
3. DESIGN PATTERNS
A reusable solution to a common problem
A template or description for how to solve a problem
A practice that has been tried
A Fantastic beast, not mystical
4. A BOOK BY “GANG OF FOUR”
Only proven design patterns
Applicable for Object-Oriented Software
5. THE PROBLEM
A simple CRUD problem
We have many transactions or queries
Same code at many places
9. SINGLETON
Ensure there is only one instance in the whole application
Has a global access point, allow easy access
May hide the constructor
10. THE PROBLEM
There are many Brand Name SMS Service Providers
We need at least 2 usable service (1 for backup)
Each service has different interface
The switching should be as quick as possible
17. FACTORY METHOD
Create objects without exposing its constructor
Create objects of the same interfaces, with subclasses define which
class to create instance
Flexible object creating
20. CONCLUSION
It’s a template, or a description, not a module or library
It can be extended, modified to suit your project and your problem
Many patterns can be sew together
It’s a matter of architecture
Các bạn đã đọc Harry Potter chưa, nếu đã đọc thì đã biết đến cuốn Fantastic Beast and where to find them
Đã được dựng thành phim năm 2016
Sách liệt kê danh sách các sinh vật “huyền bí”
Design pattern là gì? Nó chỉ là một giải pháp cho một số vấn đề lặp đi lặp lại.
Nó không đơn giản là để cho code đẹp hơn hay dễ mở rộng hơn.
Cũng không phải là điều bí ẩn hay cao siêu mà là những lời giải đã được thực hiện trước đó, và là “kỳ diệu” chứ không phải “huyền bí”
Chúng ta sẽ tìm hiểu một số Design Pattern trong quyển Design Patterns: Elements of Reusable Object-Oriented Software của bộ tứ Siêu Đẳng
Tôi sử dụng ngôn ngữ PHP để thực hiện demo
Trong cùng một session có thể có 2 đoạn thao tác với database như sau
Open many connection for one request
Don’t repeat yourself
Keep only one connection
Final refactor
Gửi tin nhắn báo thành công
Đây chỉ là một tin nhắn
Chỉ với 2 sms provider
Không quản lý được các thông tin khác như giới hạn số tin nhắn gửi đi, tần suất gửi trên phút,…
Thực ra có thể tạo ra một interface chung cho các lớp khác nhau, thậm chí hoàn toàn khác
Tạo ra các instance
Kiến trúc giữa các lớp, thực thể
Cách thức giao tiếp giữa các thực thể
Quản lý, sử dụng multi-threading