Langsung ke konten
Skillpedia
Pemrograman umum41k · #6760

python-design-patterns

Fungsinya

Skill ini membantu Anda mengatur kode Python agar lebih mudah dipahami, diuji, dan diubah. Skill ini mengajarkan prinsip-prinsip seperti menjaga kesederhanaan dan memberi setiap bagian kode satu tugas utama.

Kapan dipakai

  • ✓Saat memulai proyek baru atau bagian dari proyek.
  • ✓Saat kode yang ada terlalu berantakan dan sulit diubah.
  • ✓Saat memutuskan apakah perlu membuat struktur baru atau menulis ulang kode yang sama.

Link skill

https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-design-patterns/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-design-patterns/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Bagaimana cara mengatur kode Python saya agar lebih mudah dipelihara?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill ini menyarankan untuk mengikuti prinsip seperti 'buat sederhana' dan 'setiap bagian memiliki satu tanggung jawab'. Skill ini juga mengajarkan untuk menggabungkan objek alih-alih membuat hierarki yang rumit, dan menunggu sampai ada tiga contoh sebelum membuat struktur baru.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

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

  1. Keep it simple - Choose the simplest solution that works
  2. Single responsibility - Each unit has one reason to change
  3. Separate concerns - Distinct layers with clear purposes
  4. Compose, don't inherit - Combine objects for flexibility
  5. Rule of three - Wait before abstracting
  6. Keep functions small - 20-50 lines (varies by complexity), one purpose
  7. Inject dependencies - Constructor injection for testability
  8. Delete before abstracting - Remove dead code, then consider patterns
  9. Test each layer - Isolated tests for each concern
  10. 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

Pertanyaan umum

Apa arti 'composition over inheritance'?

Artinya, lebih baik membangun fungsi dengan menggabungkan objek yang sudah ada, daripada membuat class baru yang mewarisi class lama.

Kapan saya sebaiknya membuat abstraksi baru?

Aturan umumnya adalah menunggu sampai ada tiga contoh yang serupa. Sebelum itu, mengulang sebagian kode mungkin lebih sederhana dan lebih aman daripada membuat abstraksi terlalu dini.

Kode saya semakin besar dan berantakan. Apa yang harus saya lakukan?

Pecah kode menjadi bagian-bagian yang lebih kecil, di mana setiap bagian hanya memiliki satu alasan untuk diubah. Dengan begitu, setiap bagian menjadi lebih fokus dan lebih mudah dikelola.

Detail

Deskripsi asli dari pembuat

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

Popularitas

Bintang GitHub
41k
Peringkat
#6760
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa