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

อินเทอร์เฟซ Gateway สำหรับระบบภายนอก

ออกแบบอินเทอร์เฟซ Gateway เพื่อสื่อสารกับบริการภายนอก เช่น API คิวข้อความ หรือไลบรารีจากผู้ให้บริการรายอื่น

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

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

What are Gateway Interfaces?

In Clean Architecture, we want our core business logic to be independent of external details. This means not directly relying on specific databases, UI frameworks, or even external services.

Gateway Interfaces are your solution for communicating with these external systems without tightly coupling your core application to them.

Avoiding Direct External Calls

Imagine your core logic directly calls an external email API. What happens if that API changes, or you want to switch providers?

  • Your core code breaks.
  • Testing becomes hard, needing real API calls.
  • Your application is "coupled" to that specific external service.

Coupling makes your system rigid and difficult to change.

Gateways and the Dependency Rule

Remember the Dependency Rule in Clean Architecture? Dependencies must always point inwards, towards the core business logic.

Gateway interfaces live in an inner layer (like Use Cases), while their implementations live in an outer layer (Frameworks/Drivers).

This allows inner layers to define what they need from an external system, without knowing how it's done.

Designing a Gateway Interface

Let's define a simple EmailGateway interface. This interface declares the operations our core application needs for sending emails, without caring about the email provider.

It's just a contract!

package application.ports; // Inner layer

public interface EmailGateway {
  void sendEmail(String recipient, String subject, String body);
}

Bringing the Gateway to Life

Now, an outer layer (like an adapter for a specific email service) will implement this interface. For demonstration, we'll create a MockEmailGateway.

This is where the actual interaction with an external system would happen.

package infrastructure.adapters; // Outer layer

import application.ports.EmailGateway;

public class MockEmailGateway implements EmailGateway {
  @Override
  public void sendEmail(String recipient, String subject, String body) {
    System.out.println("--- Mock Email Service ---");
    System.out.println("To: " + recipient);
    System.out.println("Subject: " + subject);
    System.out.println("Body: " + body);
    System.out.println("Email sent successfully (mocked).");
    System.out.println("--------------------------");
  }
}

Use Case Interacts with Gateway

Our Use Case, SendWelcomeEmailUseCase, only knows about the EmailGateway interface. It doesn't care if it's a mock, a real Gmail API, or SendGrid.

This is dependency inversion in action!

package application.usecases; // Inner layer

import application.ports.EmailGateway;

public class SendWelcomeEmailUseCase {
  private final EmailGateway emailGateway;

  public SendWelcomeEmailUseCase(EmailGateway emailGateway) {
    this.emailGateway = emailGateway;
  }

  public void execute(String userEmail, String userName) {
    String subject = "Welcome to CoddyKit, " + userName + "!";
    String body = "Hello " + userName + ",\n\n"
                + "Thanks for joining CoddyKit!\n"
                + "We're excited to have you.";
    emailGateway.sendEmail(userEmail, subject, body);
  }
}

Gateway Integration Demo

Let's see the full picture. Our Main class (part of the outer Frameworks/Drivers layer) creates the concrete MockEmailGateway and injects it into the SendWelcomeEmailUseCase.

Try running this example!

public class Main {

  // Define the Gateway interface (conceptually in application.ports)
  public interface EmailGateway {
    void sendEmail(String recipient, String subject, String body);
  }

  // Define the Use Case (conceptually in application.usecases)
  public static class SendWelcomeEmailUseCase {
    private final EmailGateway emailGateway;

    public SendWelcomeEmailUseCase(EmailGateway emailGateway) {
      this.emailGateway = emailGateway;
    }

    public void execute(String userEmail, String userName) {
      String subject = "Welcome to CoddyKit, " + userName + "!";
      String body = "Hello " + userName + ",\n\n"
                  + "Thanks for joining CoddyKit!\n"
                  + "We're excited to have you.";
      emailGateway.sendEmail(userEmail, subject, body);
    }
  }

  // Define the concrete Gateway implementation (conceptually in infrastructure.adapters)
  public static class MockEmailGateway implements EmailGateway {
    @Override
    public void sendEmail(String recipient, String subject, String body) {
      System.out.println("--- Mock Email Service ---");
      System.out.println("To: " + recipient);
      System.out.println("Subject: " + subject);
      System.out.println("Body: " + body);
      System.out.println("Email sent successfully (mocked).");
      System.out.println("--------------------------");
    }
  }

  public static void main(String[] args) {
    // 1. Create the concrete Gateway implementation (outer layer)
    EmailGateway emailGateway = new MockEmailGateway();

    // 2. Create the Use Case, injecting the Gateway (inner layer)
    SendWelcomeEmailUseCase useCase =
        new SendWelcomeEmailUseCase(emailGateway);

    // 3. Execute the Use Case
    useCase.execute("john.doe@example.com", "John Doe");
  }
}

Why Use Gateway Interfaces?

Using Gateway Interfaces brings many advantages:

  • Testability: Easily swap real services with mocks for testing.
  • Flexibility: Change email providers without touching core logic.
  • Isolation: Core business rules stay clean, unaware of external tech.
  • Maintainability: Easier to update or debug external integrations.

Gateways vs. Repositories

You might notice Gateways sound similar to Repositories. Both abstract external concerns, but they have different focuses:

  • Repositories: Abstract data persistence (e.g., database operations).
  • Gateways: Abstract external services (e.g., APIs, message queues, file systems).

They both help maintain the Dependency Rule by defining interfaces in inner layers.

Test Your Knowledge

Which of the following is the primary benefit of using Gateway Interfaces in Clean Architecture?

Recap: Gateway Power

You've learned about Gateway Interfaces, a crucial pattern in Clean Architecture!

  • They abstract interactions with external services (APIs, queues).
  • They ensure your core logic remains independent and testable.
  • They uphold the Dependency Rule by defining contracts in inner layers.

Keep your core clean and let Gateways handle the outside world!

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

บทเรียน “อินเทอร์เฟซ Gateway สำหรับระบบภายนอก” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “อินเทอร์เฟซ Gateway สำหรับระบบภายนอก”

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

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

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

บทเรียน “อินเทอร์เฟซ Gateway สำหรับระบบภายนอก” ใช้เวลานานแค่ไหน

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

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

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

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

  1. รูปแบบ Repository ในสถาปัตยกรรมสะอาด
  2. อินเทอร์เฟซ Gateway สำหรับระบบภายนอก
  3. ตัวแมปข้อมูลและ DTO
  4. ชั้นป้องกันการปนเปื้อนสำหรับ API บุคคลที่สาม
← กลับไปที่ Clean Architecture & Design Patterns in Practice