python-design-patterns
Skill này làm gì
Skill này giúp tổ chức code Python để dễ hiểu, dễ kiểm thử và dễ thay đổi hơn. Nó dạy các nguyên tắc như giữ mọi thứ đơn giản và giao mỗi đoạn code một nhiệm vụ duy nhất.
Khi nào nên dùng
- ✓Khi bạn bắt đầu một dự án mới hoặc một phần của dự án.
- ✓Khi code hiện có quá rối và khó thay đổi.
- ✓Khi bạn quyết định nên tạo cấu trúc mới hay lặp lại code.
Liên kết skill
https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-design-patterns/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để tổ chức code Python của tôi cho dễ bảo trì hơn?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill đề xuất các nguyên tắc như “giữ đơn giản” và “mỗi phần có một trách nhiệm duy nhất”. Nó cũng dạy cách kết hợp các đối tượng thay vì tạo cấu trúc kế thừa phức tạp, và chờ đến khi có ba ví dụ tương tự mới tạo một cấu trúc trừu tượng mới.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Python Design Patterns
Write maintainable Python code using fundamental design principles. These patterns help you build systems that are easy to understand, test, and modify.
When to Use This Skill
- Designing new components or services
- Refactoring complex or tangled code
- Deciding whether to create an abstraction
- Choosing between inheritance and composition
- Evaluating code complexity and coupling
- Planning modular architectures
Core Concepts
1. KISS (Keep It Simple)
Choose the simplest solution that works. Complexity must be justified by concrete requirements.
2. Single Responsibility (SRP)
Each unit should have one reason to change. Separate concerns into focused components.
3. Composition Over Inheritance
Build behavior by combining objects, not extending classes.
4. Rule of Three
Wait until you have three instances before abstracting. Duplication is often better than premature abstraction.
Quick Start
# Simple beats clever
# Instead of a factory/registry pattern:
FORMATTERS = {"json": JsonFormatter, "csv": CsvFormatter}
def get_formatter(name: str) -> Formatter:
return FORMATTERS[name]()
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices Summary
- Keep it simple - Choose the simplest solution that works
- Single responsibility - Each unit has one reason to change
- Separate concerns - Distinct layers with clear purposes
- Compose, don't inherit - Combine objects for flexibility
- Rule of three - Wait before abstracting
- Keep functions small - 20-50 lines (varies by complexity), one purpose
- Inject dependencies - Constructor injection for testability
- Delete before abstracting - Remove dead code, then consider patterns
- Test each layer - Isolated tests for each concern
- Explicit over clever - Readable code beats elegant code
Troubleshooting
A class is growing and seems to have multiple responsibilities, but splitting it feels wrong. Apply the "reason to change" test: list every change that could require editing this class. If the list has items from different domains (e.g., HTTP parsing AND business rules AND formatting), split it. If all changes stem from the same domain concern, the class may be appropriately sized.
Injecting all dependencies through the constructor is producing constructors with 7+ parameters. This is a sign of too many responsibilities in one class, not a problem with dependency injection. Split the class into smaller units first, then each constructor naturally becomes smaller.
Composition is producing deeply nested wrapper objects that are hard to trace. Keep the composition shallow (2-3 levels). If wrapping is the only mechanism, consider whether a Protocol-based approach or simple function composition would be cleaner than a chain of decorator objects.
The rule of three says not to abstract yet, but the duplication is causing bugs when one copy is updated but not the other. Duplication that diverges in dangerous ways should be abstracted sooner. The rule of three is a heuristic, not a law. If the copies are already diverging incorrectly, extract immediately and add a test that exercises the shared behavior.
A service layer is importing from the API layer, breaking the dependency direction. This is a layering violation. The service layer must not import from handlers. Introduce a shared types/models layer that both can import from, keeping the dependency arrow pointing downward (API → Service → Repository).
Related Skills
- python-testing-patterns — Test each layer in isolation using the dependency injection structure established here
- python-project-structure — Organize modules and directory layout so layer boundaries are explicit from the start
Câu hỏi thường gặp
“Composition over inheritance” nghĩa là gì?
Nghĩa là tốt hơn là xây dựng chức năng bằng cách kết hợp các đối tượng có sẵn, thay vì tạo các class mới kế thừa từ class cũ.
Khi nào nên tạo một abstraction mới?
Quy tắc chung là chờ đến khi có ba ví dụ tương tự. Trước đó, lặp lại một đoạn code đôi khi đơn giản và an toàn hơn so với tạo một abstraction quá sớm.
Code của tôi quá lớn và rối. Tôi nên làm gì?
Hãy chia code thành các phần nhỏ hơn, mỗi phần chỉ có một lý do để thay đổi. Điều này làm mỗi phần tập trung hơn và dễ quản lý hơn.
Chi tiết
Mô tả gốc của tác giả
Python design patterns including KISS, Separation of Concerns, Single Responsibility, and composition over inheritance. Use this skill when designing a new service or component from scratch and choosing how to layer responsibilities, when refactoring a God class or monolithic function that has grown too large, when deciding whether to add a new abstraction or live with duplication, when evaluating
repo: wshobson/agents
Độ phổ biến
- Sao GitHub
- 41k
- Vị trí xếp hạng
- #6760
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
screen-reader-testing
41kGiúp agent của bạn kiểm thử website bằng trình đọc màn hình để đảm bảo chúng có thể truy cập được.
wcag-audit-patterns
41kGiúp agent của bạn kiểm toán website về khả năng truy cập và sửa các vấn đề theo hướng dẫn WCAG.
multi-reviewer-patterns
41kCho phép agent của bạn điều phối các đợt rà soát mã song song, loại bỏ phát hiện trùng lặp và tổng hợp báo cáo.
parallel-debugging
41kCho phép agent gỡ lỗi các vấn đề phức tạp bằng cách điều tra song song và phân tích nguyên nhân gốc.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.