quantity: Integer
는 private int quantity;
로 매핑된다.description: ProductDescription
는 private ProductDescription description;
로 매핑된다.getSubtotal(): Money
는 public Money getSubtotal() { ... }
로 매핑된다.enterItem
메시지가 Register
인스턴스로 보내진다. 따라서, enterItem
메서드는 Register
클래스에 정의된다.ProductCatalog
객체에 getProductDescription
메시지를 보내 제품 설명을 가져온다.Sale
객체에 makeLineItem
메시지를 보내 판매 항목을 생성한다.enterItem
메서드를 작성한다.
public class Register {
private ProductCatalog catalog;
private Sale currentSale;
public Register(ProductCatalog catalog) {
this.catalog = catalog;
this.currentSale = new Sale();
}
public void enterItem(int itemID, int qty) {
ProductDescription desc = catalog.getProductDescription(itemID);
currentSale.makeLineItem(desc, qty);
}
}
객체 지향 프로그래밍에서 다수의 객체를 관리하고 조작하기 위한 중요한 도구 특히일대 다관계를 표현하는 데 사용된다.
클래스는 결합도가 낮은 것에서 높은 것으로 구현되어야 한다.