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

พอร์ตข้อมูลเข้าและข้อมูลออก

ทำความเข้าใจว่าพอร์ตข้อมูลเข้าและข้อมูลออก (อินเทอร์เฟซ) กำหนดขอบเขตการสื่อสารระหว่างกรณีใช้งานกับเลเยอร์ภายนอกอย่างไร

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

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

What are Input & Output Ports?

In Clean Architecture, "Ports" are crucial interfaces that define how different layers communicate. They act like contracts.

Think of them as the "sockets" of your application's core logic. They specify what goes in (Input) and what comes out (Output).

Defining Input Ports

An Input Port is an interface defined by the Use Case layer. It declares the methods that an external layer (like a UI controller) can call to interact with the Use Case.

  • It's the "what you can do" contract for the Use Case.
  • It ensures the Use Case doesn't know about the UI or external callers.

Input Port: User Login

Here's a simple Input Port for a user login feature. It defines the operations the Use Case expects.

public interface UserLoginInputPort {
  void login(String username, String password);
}

public class UserLoginUseCase implements UserLoginInputPort {
  @Override
  public void login(String username, String password) {
    // Business logic for login
    System.out.println("Attempting login for: " + username);
    if (username.equals("admin") && password.equals("pass")) {
      System.out.println("Login successful!");
    } else {
      System.out.println("Login failed.");
    }
  }

  public static void main(String[] args) {
    UserLoginInputPort loginService = new UserLoginUseCase();
    loginService.login("admin", "pass");
    loginService.login("user", "123");
  }
}

Defining Output Ports

An Output Port is also an interface, defined by the Use Case layer. It declares the methods that the Use Case will call to deliver results or interact with external services (like a database or UI presenter).

  • It's the "what I need to tell you" contract.
  • It keeps the Use Case unaware of specific implementations (e.g., how results are displayed).

Output Port: Login Presenter

This Output Port defines how the login result should be presented or handled. The Use Case calls these methods, but doesn't know how they are implemented.

public interface UserLoginOutputPort {
  void presentLoginSuccess(String username);
  void presentLoginFailure(String message);
}

public class ConsolePresenter implements UserLoginOutputPort {
  @Override
  public void presentLoginSuccess(String username) {
    System.out.println("Welcome, " + username + "!");
  }

  @Override
  public void presentLoginFailure(String message) {
    System.out.println("Error: " + message);
  }

  public static void main(String[] args) {
    UserLoginOutputPort presenter = new ConsolePresenter();
    presenter.presentLoginSuccess("Alice");
    presenter.presentLoginFailure("Invalid credentials.");
  }
}

How Use Cases Use Ports

The Use Case (or Interactor) depends on these port interfaces. It takes an Input Port as its public method and receives an Output Port through its constructor or method parameters.

This means the Use Case only knows about abstractions, not concrete implementations.

Use Case Connecting Ports

Here, our UserLoginUseCase now uses both ports. Notice it doesn't know if it's talking to a web UI, a mobile app, or a console!

public interface UserLoginInputPort {
  void login(String username, String password);
}

public interface UserLoginOutputPort {
  void presentLoginSuccess(String username);
  void presentLoginFailure(String message);
}

public class UserLoginUseCase implements UserLoginInputPort {
  private final UserLoginOutputPort outputPort;

  public UserLoginUseCase(UserLoginOutputPort outputPort) {
    this.outputPort = outputPort;
  }

  @Override
  public void login(String username, String password) {
    if (username.equals("admin") && password.equals("pass")) {
      outputPort.presentLoginSuccess(username);
    } else {
      outputPort.presentLoginFailure("Invalid credentials.");
    }
  }

  public static void main(String[] args) {
    // In real app, this would be wired by a framework
    UserLoginOutputPort presenter = new ConsolePresenter(); // From Scene 5
    UserLoginInputPort loginService = new UserLoginUseCase(presenter);

    loginService.login("admin", "pass");
    loginService.login("user", "wrong");
  }
}

class ConsolePresenter implements UserLoginOutputPort {
  @Override
  public void presentLoginSuccess(String username) {
    System.out.println("Welcome, " + username + "!");
  }

  @Override
  public void presentLoginFailure(String message) {
    System.out.println("Error: " + message);
  }
}

Ports and the Dependency Rule

The use of Ports perfectly adheres to the Dependency Rule. The Use Case layer (core business logic) depends on interfaces (the Ports).

The outer layers (like UI or databases) then implement these interfaces, providing the concrete details without the core knowing about them.

Why Use Ports?

Ports offer powerful advantages:

  • Decoupling: The Use Case is isolated from UI, database, or external services.
  • Testability: You can easily mock or stub these interfaces for robust unit testing of Use Cases.
  • Flexibility: You can swap out implementations (e.g., change database) without touching core logic.
  • Clarity: They clearly define the boundaries and interactions of your application's core.

Quick Check: Port Roles

Consider the roles of Input and Output Ports in Clean Architecture.

Recap: Ports as Boundaries

We learned that Input Ports are interfaces defining what a Use Case expects, acting as entry points.

Output Ports are interfaces defining what a Use Case delivers or needs from external services.

Together, they enforce the Dependency Rule, ensuring our core business logic remains independent, testable, and flexible.

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

บทเรียน “พอร์ตข้อมูลเข้าและข้อมูลออก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พอร์ตข้อมูลเข้าและข้อมูลออก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “พอร์ตข้อมูลเข้าและข้อมูลออก” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การออกแบบเอนทิตีทางธุรกิจ
  2. การนำกรณีใช้งานมาใช้ (ตัวประสานงาน)
  3. พอร์ตข้อมูลเข้าและข้อมูลออก
  4. การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ
← กลับไปที่ Clean Architecture & Design Patterns in Practice