0Pricing
Clean Architecture & Design Patterns in Practice · 강의

입력 및 출력 포트

입력 및 출력 포트(인터페이스)가 사용 사례와 외부 계층 사이의 통신 경계를 정의하는 방식을 이해합니다.

입력 및 출력 포트은(는) CoddyKit의 무료 Clean Architecture & Design Patterns in Practice 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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.

자주 묻는 질문

“입력 및 출력 포트” 강의는 무료인가요?

네 — “입력 및 출력 포트” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clean Architecture & Design Patterns in Practice 강의 전체를 잠금 해제할 수 있습니다. Clean Architecture & Design Patterns in Practice 강의에는 총 4개의 강의가 포함되어 있습니다.

“입력 및 출력 포트”에서 뭘 배우나요?

입력 및 출력 포트(인터페이스)가 사용 사례와 외부 계층 사이의 통신 경계를 정의하는 방식을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Clean Architecture & Design Patterns in Practice을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Clean Architecture & Design Patterns in Practice을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Clean Architecture & Design Patterns in Practice은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“입력 및 출력 포트” 강의는 얼마나 걸리나요?

대부분의 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(으)로 돌아가기