비즈니스 엔터티 설계
전사적인 비즈니스 규칙을 캡슐화하고 안정적으로 유지되는 핵심 비즈니스 객체인 엔터티를 정의합니다.
비즈니스 엔터티 설계은(는) CoddyKit의 무료 Clean Architecture & Design Patterns in Practice 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Clean Architecture & Design Patterns in Practice 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Clean Architecture & Design Patterns in Practice 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What are Business Entities?
In Clean Architecture, Entities are the heart of your application. They represent the core business concepts and rules that are crucial to your enterprise.
Think of them as the fundamental building blocks that define "what your business is" and "how it works," independent of how you store or display data.
Core Business Rules
Entities encapsulate enterprise-wide business rules. These are rules that would exist even if there were no software system, like "a product must have a positive price" or "an order must have a customer."
These rules are stable and apply across your entire organization, not just a specific part of your application.
Independent & Stable
A key principle of Entities is their independence. They should not depend on external layers like databases, web frameworks, or UI components.
This makes them the most stable part of your application, changing only when the core business rules themselves change, not when technology does.
Entity vs. DTO
It's important to distinguish an Entity from a simple Data Transfer Object (DTO). A DTO is just a bag of data, often used to move data between layers.
An Entity, however, contains both data and behavior. It actively enforces its own business rules through its methods.
Entity Structure
Typically, an Entity includes:
- Attributes: The data that describes the business object (e.g., name, ID, price).
- Methods: The operations that can be performed on the entity, enforcing its business rules and maintaining its integrity.
These methods ensure the entity always remains in a valid state according to your business logic.
Example: `Product` Entity
Let's consider a simple Product entity. What data and rules would it need?
- A product needs an
id,name, andprice. - A core business rule might be: "A product's price must always be positive."
The entity itself should enforce this rule.
`Product` Entity Code
Here's how a basic Product entity might look in Java, ensuring the price rule:
public class Product {
private String id;
private String name;
private double price;
public Product(String id, String name, double price) {
if (price <= 0) {
throw new IllegalArgumentException("Price must be positive.");
}
this.id = id;
this.name = name;
this.price = price;
}
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public static void main(String[] args) {
try {
Product laptop = new Product("P001", "Laptop", 1200.00);
System.out.println(laptop.getName() + " costs $" + laptop.getPrice());
// This would throw an error:
// Product invalid = new Product("P002", "Free Item", 0.00);
} catch (IllegalArgumentException e) {
System.out.println("Error creating product: " + e.getMessage());
}
}
}Entity Behavior & Rules
Entities aren't just data containers; they have methods that encapsulate behavior and enforce rules. For example, applying a discount might have its own business logic.
The entity's methods ensure its internal state remains consistent and valid according to business rules.
`Product` Discount Example
Let's add a method to our Product entity to apply a discount, with a rule that discounts can't make the price negative.
public class Product {
private String id;
private String name;
private double price;
public Product(String id, String name, double price) {
if (price <= 0) {
throw new IllegalArgumentException("Price must be positive.");
}
this.id = id;
this.name = name;
this.price = price;
}
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
// New method to apply discount
public void applyDiscount(double percentage) {
if (percentage < 0 || percentage > 100) {
throw new IllegalArgumentException("Discount must be between 0-100%.");
}
double newPrice = this.price * (1 - percentage / 100);
if (newPrice < 0) { // Ensure price doesn't go negative
this.price = 0;
} else {
this.price = newPrice;
}
}
public static void main(String[] args) {
Product book = new Product("B001", "Clean Code Book", 40.00);
System.out.println("Original: " + book.getName() + " $" + book.getPrice());
book.applyDiscount(25); // 25% discount
System.out.println("Discounted: " + book.getName() + " $" + book.getPrice());
try {
book.applyDiscount(150); // Invalid discount
} catch (IllegalArgumentException e) {
System.out.println("Error applying discount: " + e.getMessage());
}
}
}Entity Properties Check
Based on what we've learned, which of the following are true characteristics of a Clean Architecture Entity?
Recap: Designing Entities
Great job! You've learned about Entities in Clean Architecture. Key takeaways:
- Entities represent your core business concepts.
- They encapsulate enterprise-wide business rules.
- Entities contain both data and behavior, actively enforcing their rules.
- They are independent and stable, not tied to frameworks or external layers.
Next, we'll explore how Use Cases interact with these robust entities to perform application-specific tasks.
자주 묻는 질문
“비즈니스 엔터티 설계” 강의는 무료인가요?
네 — “비즈니스 엔터티 설계” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 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개 중 1번째 강의입니다.
“비즈니스 엔터티 설계” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Clean Architecture & Design Patterns in Practice 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Clean Architecture & Design Patterns in Practice 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 비즈니스 엔터티 설계
- 사용 사례(인터랙터) 구현
- 입력 및 출력 포트
- 불변식으로 비즈니스 규칙 강제하기