Trung BìnhDesign Patterns iconDesign Patterns

Facade pattern là gì? Ứng dụng trong frontend development?

Facade cung cấp simplified interface cho một hệ thống phức tạp, subsystem hoặc library — giảm dependency giữa client code và internals phức tạp.

Ví dụ: thay vì client gọi trực tiếp 5 service (AuthService, UserService, ProfileService, CacheService, LogService), ta tạo UserFacade với method đơn giản như getUserProfile(id) tự phối hợp các service. Trong frontend: custom hook là Facade xuất sắc — useAuth() ẩn đi chi tiết của JWT storage, API call, state management; component chỉ gọi const { user, login, logout } = useAuth(). Facade không ngăn client access subsystem trực tiếp nếu cần — khác với Proxy. Dùng khi: có subsystem phức tạp cần đơn giản hóa; khi muốn layer hóa architecture (presentation → service facade → domain). Không dùng khi: tạo ra 'God Facade' ôm quá nhiều thứ — vi phạm SRP.

Xem toàn bộ Design Patterns cùng filter theo level & chủ đề con.

Mở danh sách Design Patterns