# Design Patterns ใน Django (Django) > Repository pattern, Service layer, Factory pattern, Strategy pattern, หลักการ SOLID, clean architecture - 24 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Django](https://sharpskill.dev/th/technologies/django/interview-questions.md) ## 1. วัตถุประสงค์หลักของ Service Layer pattern ในแอปพลิเคชัน Django คืออะไร? **คำตอบ** Service Layer pattern ห่อหุ้มลอจิกทางธุรกิจไว้ในเลเยอร์เฉพาะที่แยกออกจาก view และ model การแยกนี้ช่วยเพิ่มความสามารถในการทดสอบเนื่องจากลอจิกสามารถทดสอบได้อย่างอิสระ ช่วยให้นำโค้ดกลับมาใช้ซ้ำได้ระหว่าง view หรือ command ต่าง ๆ และทำให้ view บางลงโดยการมอบหมายการทำงานที่ซับซ้อนให้กับ service ซึ่งสอดคล้องกับ Single Responsibility Principle (SRP) ## 2. Repository Pattern ทำการแยกส่วน (abstract) สิ่งใดในแอปพลิเคชัน Django? **คำตอบ** Repository Pattern จัดให้มีการแยกส่วนระหว่างเลเยอร์ทางธุรกิจและเลเยอร์การเข้าถึงข้อมูล โดยห่อหุ้มคิวรี ORM ของ Django ไว้ในเมธอดที่ชัดเจน ทำให้สามารถเปลี่ยนแหล่งข้อมูลได้ง่ายโดยไม่ต้องแก้ไขลอจิกทางธุรกิจ อีกทั้งยังช่วยให้การทดสอบหน่วยทำได้ง่ายขึ้นด้วยการ mock repository แทนฐานข้อมูลจริง ## 3. หลักการ SOLID ข้อใดถูกละเมิดเมื่อ view ของ Django มีทั้งการตรวจสอบความถูกต้อง ลอจิกทางธุรกิจ และการส่งอีเมลรวมอยู่ด้วยกัน? **คำตอบ** Single Responsibility Principle (SRP) ระบุว่าคลาสหรือฟังก์ชันควรมีเหตุผลในการเปลี่ยนแปลงเพียงเหตุผลเดียว view ที่จัดการทั้งการตรวจสอบความถูกต้อง ลอจิกทางธุรกิจ และการส่งอีเมลมีความรับผิดชอบที่แตกต่างกันอย่างน้อยสามอย่าง การเปลี่ยนแปลงในด้านใดด้านหนึ่งจะต้องแก้ไข view ทำให้โค้ดเปราะบางและบำรุงรักษายาก ## มีอีก 21 คำถาม - โดยทั่วไป Factory Pattern ถูกใช้อย่างไรในแอปพลิเคชัน Django? - ข้อได้เปรียบหลักของ Strategy Pattern ในการนำวิธีการชำระเงินต่าง ๆ มาใช้ใน Django คืออะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Django อื่นๆ - [พื้นฐาน Python](https://sharpskill.dev/th/technologies/django/interview-questions/python-basics.md): 25 คำถาม, Junior - [Python เชิงวัตถุ](https://sharpskill.dev/th/technologies/django/interview-questions/python-oop.md): 20 คำถาม, Junior - [พื้นฐาน Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-fundamentals.md): 20 คำถาม, Junior - [Django Models - พื้นฐาน](https://sharpskill.dev/th/technologies/django/interview-questions/django-models-basics.md): 22 คำถาม, Junior - [Django Views](https://sharpskill.dev/th/technologies/django/interview-questions/django-views.md): 20 คำถาม, Junior - [เทมเพลต Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-templates.md): 18 คำถาม, Junior - [ฟอร์ม Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-forms.md): 22 คำถาม, Mid-Level - [QuerySets ขั้นสูง](https://sharpskill.dev/th/technologies/django/interview-questions/django-querysets.md): 25 คำถาม, Mid-Level - [การยืนยันตัวตนใน Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-authentication.md): 22 คำถาม, Mid-Level - [Django Middleware](https://sharpskill.dev/th/technologies/django/interview-questions/django-middleware.md): 18 คำถาม, Mid-Level - [Django Admin](https://sharpskill.dev/th/technologies/django/interview-questions/django-admin.md): 20 คำถาม, Mid-Level - [Django REST Framework](https://sharpskill.dev/th/technologies/django/interview-questions/django-rest-framework.md): 30 คำถาม, Mid-Level - [Django Signals](https://sharpskill.dev/th/technologies/django/interview-questions/django-signals.md): 18 คำถาม, Mid-Level - [การอัปโหลดไฟล์](https://sharpskill.dev/th/technologies/django/interview-questions/django-file-upload.md): 20 คำถาม, Mid-Level - [การแคชใน Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-caching.md): 20 คำถาม, Mid-Level - [Django Sessions](https://sharpskill.dev/th/technologies/django/interview-questions/django-sessions.md): 18 คำถาม, Mid-Level - [อีเมลใน Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-email.md): 18 คำถาม, Mid-Level - [การทดสอบ Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-testing.md): 22 คำถาม, Mid-Level - [ความปลอดภัยของ Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-security.md): 22 คำถาม, Mid-Level - [การ Deploy Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-deployment.md): 24 คำถาม, Mid-Level - [Django ORM ขั้นสูง](https://sharpskill.dev/th/technologies/django/interview-questions/django-orm-advanced.md): 28 คำถาม, Senior - [ประสิทธิภาพของ Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-performance.md): 24 คำถาม, Senior - [Django & Celery](https://sharpskill.dev/th/technologies/django/interview-questions/django-celery.md): 22 คำถาม, Senior - [Django Channels](https://sharpskill.dev/th/technologies/django/interview-questions/django-channels.md): 24 คำถาม, Senior - [Django & GraphQL](https://sharpskill.dev/th/technologies/django/interview-questions/django-graphql.md): 24 คำถาม, Senior - [Django & Docker](https://sharpskill.dev/th/technologies/django/interview-questions/django-docker.md): 20 คำถาม, Senior - [Django ในระบบนิเวศ Microservices](https://sharpskill.dev/th/technologies/django/interview-questions/django-microservices.md): 24 คำถาม, Senior - [คำสั่ง Django แบบกำหนดเอง](https://sharpskill.dev/th/technologies/django/interview-questions/django-custom-commands.md): 18 คำถาม, Senior - [การทำให้รองรับหลายภาษาใน Django](https://sharpskill.dev/th/technologies/django/interview-questions/django-internationalization.md): 20 คำถาม, Senior - [Django Async และ ASGI](https://sharpskill.dev/th/technologies/django/interview-questions/django-async-asgi.md): 26 คำถาม, Senior - [Settings และการตั้งค่า Production](https://sharpskill.dev/th/technologies/django/interview-questions/django-settings-configuration.md): 22 คำถาม, Senior - [Observability และ Monitoring](https://sharpskill.dev/th/technologies/django/interview-questions/django-observability.md): 24 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/django/interview-questions/django-design-patterns