본문 바로가기

Dev.../소프트웨어 아키텍처

[SA강좌] Part 4-19 Embedded Value 패턴

내재된 값(Embedded Value) 패턴

내재된 값 패턴의 정의

내재된 값 패턴은 개체를 다른 개체의 테이블의 몇몇 필드들에 매핑한다.

그림 -26. 내재된 패턴의 구조

내재된 값 패턴의 설명

많은 작은 개체들은 테이터베이스의 테이블들 처럼 의미적이지 않은 개체지향 시스템 안에서 의미있게 한다. 예로 동시 발생 인식 메모리 개체와 일자 범위를 포함한다. 비록 기본적 생각은 개체를 테이블과 같이 저장하고, 같지 않은 사원은 메모리 값을 테이블이 원한다.

내재된 값은 개체의 값을 개체 소유자의 레코드에 있는 필드에 매핑한다.

내재된 값 패턴의 예제: 값 개체

내포된 값과 매핑되는 값 개체의 예는 아래와 같다. 다음과 같은 필드들을 가진 간단한 요청 클래스로 시작한다.

 

class ProductOffering ...

 

private Product product;

private Money baseCost;

private Integer ID;

위의 필드에서 ID 필드는 일치 필드(Identtity Field) 패턴을 사용하고, product 는 일반적 레코드 매핑이다.

 

class ProductOffering ...

 

public static ProductOffering load(ResultSet rs) {

try {

Integer id = (Integer) rs.getObject("ID");

BigDecimal baseCostAmount = rs.getBigDecimal("base_cost_amount");

Currency baseCostCurrency = Registry.getCurrency(rs.getString("base_cost_currency"));

Money baseCost = new Money(baseCostAmount, baseCostCurrency);

Integer productID = (Integer) rs.getObject("product");

Product product = Product.find(Integer) rs.getObject("product"));

return new ProductOffering(id, product, baseCost);

} catch(SQLException e) {

throw new ApplicationExcetion(e);

}

}

아래는 업데이트 행동이다. 다시 업데이터에서 간단한 변수이다.

 

class ProductOffering ...

 

public void update() {

PreparedStatment stmt = null;

try {

stmt = DB.prepare(updateStatmentString);

stmt.setBigDecimal(1, baseCost.amount);

stmt.setString(2, baseCost.currency().code());

stmt.setInt(3, ID.intValue());

stmt.execute();

} catch(Exception e) {

throw new ApplicationException(e);

} finally { DB.cleanUp(stmt);

}

private String updateStatementString =

" UPDATE product_offerings " +

" SET base_cost_amount = ?, base_cost_currency = ? " +

" WHERE id = ? ";