0Pricing
Clean Architecture & Design Patterns in Practice · บทเรียน

การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง

เรียนรู้ว่าการฉีดทรัพยากรพึ่งพาช่วยเสริมรูปแบบการสร้างด้วยการย้ายการสร้างออบเจ็กต์ออกจากผู้ใช้ทรัพยากร และนำไปไว้ที่จุดประกอบส่วนกลางได้อย่างไร

การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clean Architecture & Design Patterns in Practice และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Another Creational Approach?

Classic creational patterns like Factory and Builder centralize how objects are built. Dependency Injection (DI) centralizes where they are wired together.

  • A class no longer creates its own collaborators.
  • Instead, collaborators are supplied from outside.

This is the natural endpoint of the creational journey: removing the new keyword from business logic entirely.

The Problem DI Solves

Consider a service that builds its own dependency:

The service is now hard-wired to a concrete class. You cannot swap it for a test double or an alternative implementation without editing the service.

class ReportService {
    private final MySqlDatabase db = new MySqlDatabase();
    void run() { db.query("..."); }
}

Constructor Injection

The most common and recommended form: dependencies arrive through the constructor.

  • The class declares what it needs, not how to get it.
  • Dependencies become final and guaranteed present.
class ReportService {
    private final Database db;
    ReportService(Database db) { this.db = db; }
    void run() { db.query("..."); }
}

Inversion of Control

DI is a concrete technique for the broader principle of Inversion of Control (IoC): the flow of object creation is inverted away from the consumer.

The consumer no longer asks I will build my tools; instead it says give me what I need. A higher layer decides the wiring.

The Composition Root

All wiring happens in one place near the program entry point, called the composition root.

This is where concrete classes are finally chosen and assembled into the object graph.

public static void main(String[] args) {
    Database db = new MySqlDatabase();
    ReportService service = new ReportService(db);
    service.run();
}

DI vs Factory

They are complementary, not competitors:

  • A Factory decides which concrete object to build at runtime.
  • DI passes already-built objects into consumers.

A composition root often uses factories to produce the objects it then injects.

Setter and Method Injection

Two other forms exist for optional dependencies:

  • Setter injection: a dependency is set after construction.
  • Method injection: a dependency is passed to a single method call.

Prefer constructor injection for required collaborators; reserve these for truly optional ones.

class Logger { void log(String m) {} }
class Job {
    private Logger logger;
    void setLogger(Logger l) { this.logger = l; }
}

DI Containers

Frameworks like Spring or Guice provide a DI container that auto-resolves the object graph based on registered types.

The container is your composition root, automated. But the principle is identical: construction is centralized and consumers stay clean.

Testability Benefit

Because dependencies are injected, tests can pass mocks or fakes directly.

class FakeDatabase implements Database {
    public void query(String s) { /* record call */ }
}

// in test
ReportService s = new ReportService(new FakeDatabase());

Avoiding the Service Locator Anti-Pattern

A tempting shortcut is a global service locator that classes call to fetch dependencies.

This hides dependencies again and reintroduces coupling. Prefer explicit injection so a class signature truthfully declares everything it needs.

Guidelines for Clean Construction

  • Push new to the composition root.
  • Depend on abstractions, inject implementations.
  • Use constructor injection by default.
  • Keep constructors free of logic; only assign fields.

Quick Check

Test your understanding of dependency injection.

Recap

You learned that Dependency Injection extends the creational toolkit by removing object construction from consumers.

  • Constructor injection is the default form.
  • Wiring lives in the composition root.
  • DI complements factories and dramatically improves testability.

You now have a complete picture of creating objects cleanly.

คำถามที่พบบ่อย

บทเรียน “การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง”

เรียนรู้ว่าการฉีดทรัพยากรพึ่งพาช่วยเสริมรูปแบบการสร้างด้วยการย้ายการสร้างออบเจ็กต์ออกจากผู้ใช้ทรัพยากร และนำไปไว้ที่จุดประกอบส่วนกลางได้อย่างไร คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม

ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Singleton และ Factory Method
  2. Abstract Factory และ Builder
  3. Prototype และ Object Pool
  4. การฉีดทรัพยากรพึ่งพาในฐานะเทคนิคการสร้าง
← กลับไปที่ Clean Architecture & Design Patterns in Practice