
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);
}
}在面向对象编程中,用于管理和操作多个对象的重要工具。 尤其常用于表达一对多关系。
应当按照耦合度从低到高的顺序实现类。
