1. Value Object (VO)
- 정의: VO는 값 자체를 표현하는 객체로, 불변성을 갖습니다. VO는 주로 데이터의 특정 조합을 표현하며, 데이터가 같다면 동일한 것으로 간주됩니다.
- 특징: VO는 일반적으로 변경이 불가능하고, 동일성이 아니라 동등성에 의해 비교됩니다. 예를 들어, 두 주소 VO가 같은 주소를 표현한다면, 서로 다른 객체일지라도 동등하다고 판단됩니다.
ex)
public final class Address {
private final String street;
private final String city;
private final String zipCode;
public Address(String street, String city, String zipCode) {
this.street = street;
this.city = city;
this.zipCode = zipCode;
}
// Getters and methods to compare addresses based on values
public String getStreet() { return street; }
public String getCity() { return city; }
public String getZipCode() { return zipCode; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Address address = (Address) o;
return street.equals(address.street) && city.equals(address.city) && zipCode.equals(address.zipCode);
}
@Override
public int hashCode() {
return Objects.hash(street, city, zipCode);
}
}
record를 사용하여 바꿀수 있음
public record Address (String street, String zipCode) {}
2. Data Access Object (DAO)
- 정의: DAO는 데이터베이스나 다른 저장 매체로부터 데이터를 조회하거나 조작하는 역할을 담당하는 객체입니다. DAO는 데이터 접근 로직과 비즈니스 로직을 분리하여 데이터베이스 코드와 비즈니스 코드 간의 의존성을 줄입니다.
- 특징: DAO는 CRUD(Create, Read, Update, Delete) 작업을 추상화하며, 특정 데이터베이스 기술에 종속되지 않는 인터페이스를 제공합니다.
public interface ProductDao {
List<Product> findAll();
Product findById(int id);
void save(Product product);
void update(Product product, String[] params);
void delete(Product product);
}
public class ProductDaoImpl implements ProductDao {
private List<Product> products = new ArrayList<>();
@Override
public List<Product> findAll() {
// Return all products
return new ArrayList<>(products);
}
@Override
public Product findById(int id) {
// Return a product by ID
return products.stream().filter(product -> product.getId() == id).findFirst().orElse(null);
}
}
3. Data Transfer Object (DTO)
- 정의: DTO는 계층 간 데이터 교환을 위해 사용되는 객체로, 여러 데이터를 하나의 객체로 묶어 네트워크를 통해 데이터를 전송하는 데 사용됩니다.
- 특징: DTO는 로직을 포함하지 않으며 순수하게 데이터 전달만을 목적으로 합니다. 서로 다른 계층 간에 데이터를 운반하는 용도로 사용됩니다.
public class ProductDTO {
private int id;
private String name;
private double price;
public ProductDTO(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setPrice(double price) { this.price = price; }
}
4. Entity
- 정의: Entity는 데이터베이스 테이블의 행(row)을 객체 지향적으로 표현한 것으로, 일반적으로 식별자(ID)를 통해 구별됩니다.
- 특징: Entity는 데이터베이스의 엔티티와 직접적으로 매핑되며, 영속성을 가진 데이터를 표현합니다. Entity는 그 자체로 비즈니스 로직을 포함할 수 있고, 변경 가능한 상태를 유지할 수 있습니다.
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private double price;
// Constructors, getters, setters
public int getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public void setName(String name) { this.name = name; }
public void setPrice(double price) { this.price = price; }
}
5. Domain
- 정의: Domain은 특정 비즈니스 문제 영역을 모델링한 것으로, 비즈니스 로직과 데이터를 포함합니다.
- 특징: Domain 모델은 소프트웨어가 해결하려는 비즈니스 영역의 복잡성을 캡슐화하며, 비즈니스 객체 간의 관계, 상호 작용, 규칙을 정의합니다.
public class Product {
private String name;
private double price;
private int stockQuantity;
public Product(String name, double price, int stockQuantity) {
this.name = name;
this.price = price;
this.stockQuantity = stockQuantity;
}
public void applyDiscount(double discountRate) {
this.price -= this.price * discountRate;
}
}
반응형