14
Domain Model—Part 4A: Special associations - Composition

Domain Model—Part 4A: Special associations - Composition

Embed Size (px)

Citation preview

Domain Model—Part 4A:Special associations -

Composition

Product

productIDproductNameingredientSet : Ingredientcategory : Category

OrderLine

product : Productquantity

11..* 11..*

orders quantity of

Order

orderIDorderDateorderTotalcustomer : CustomerproductOrderedSet : OrderLine

1..*

1

1..*

1

contains

ORDER contains many OrderLine objects.OrderLine objects have no “life” outside of

order.OrderLine objects have no meaning

outside of order. If you delete Order, all OrderLine objects

will be gone.Order “encapsulates” OrderLine.

What does “Order ‘encapsulates’ OrderLine mean? Order takes responsibility for creation of

OrderLine objects Order controls all communication to OrderLine

objects No other class knows that OrderLine exists But OrderLine knows about other classes (e.g.

Product) OrderLine might not even appear in higher level

models; only Order.

public class Order {

private int orderID;private Customer customer;private List<OrderLine> productOrderedSet;

public Order(int newOrdID) {orderID = newOrdID;customer = new Customer();productOrderedSet = new ArrayList<OrderLine>();

}

public Order() {orderID = 0;customer = new Customer();productOrderedSet = new ArrayList<OrderLine>();}

public void addOrderLine(int inQty, Product inProd) {OrderLine newOrderLine = new OrderLine(inQty,inProd);productOrderedSet.add(newOrderLine);

}public String getOrderedProduct(int orderLineInd) {

return productOrderedSet.get(orderLineInd).getProductName();}public int getOrderedQty(int orderLineInd) {

return productOrderedSet.get(orderLineInd).getQty();}

public void addCustomer(Customer setCustomerTo) {this.customer=setCustomerTo;

}

public String getCustName() {return customer.getCustName();

}

public int getOrderID() {return orderID;

}

}

The Order class(simplified version)

public class Order {

private int orderID;private Customer customer;private List<OrderLine> productOrderedSet;

…………………………………………………………..

public void addOrderLine(int inQty, Product inProd) {OrderLine newOrderLine = new OrderLine(inQty,inProd);productOrderedSet.add(newOrderLine);

}public String getOrderedProduct(int orderLineInd) {

return productOrderedSet.get(orderLineInd).getProductName();}public int getOrderedQty(int orderLineInd) {

return productOrderedSet.get(orderLineInd).getQty();}

………………………………………………………………

}

Order is responsible for creating OrderLine objects and for getting OrderLine information

public class OrderLine {

private int qtyOrdered;private Product product;

public OrderLine(int qty,Product prod) {qtyOrdered = qty;this. product = prod;}

public OrderLine() {qtyOrdered = 0;product = new Product();}

public int getQty() {return qtyOrdered;

}public String getProductName() {

return product.getProductName();

}}

OrderLine knows about Product and can execute Product functions (e.g. product.GetProductName)

public class Product {

private int productID;private String productName;

public Product(int setToID,String setToName) {productID = setToID;productName = setToName;

}public Product() {}

public String getProductName() {return productName;

}public int getProductID() {

return productID;}public void setProductName(String setToName) {

productName = setToName;}public void setProductID(int setToID) {

productID = setToID;}}

Product does not know anything about OrderLine; it does not know that OrderLine exists.

Order

orderIDorderDateorderTotalcustomer : CustomerproductOrderedSet : OrderLine

OrderLine

product : Productquantity

1..*

1

1..*

1

contains

This strong relationship between Order and OrderLine is called a COMPOSITION and is shown using the “filled in diamond” symbol.

Order is the container.

OrderLine is the component or contained class.

In order to specify composition we need to do two things: First we use the aggregation symbol in Rose

to create a relationship between Container (Order) and Component (OrderLine). We draw from Order to OrderLine.

Then we select the “aggregate” relationship that we just drew and make it a composition by selecting “containment of OrderLine” and choosing “by value”.

The aggregation symbol on its own is used to denote a weaker relationship—weaker than composition but stronger than association.

We will not be exploring this relationship in SYS466. It has limited use in modeling and has no definite “meaning” when translated to code.

In SYS466 we will only be identifying composition in the “pattern” exemplified by Order/OrderLine, Sales/SaleLine, Bill/BillingItem, etc.

It is easier because those are the relationships that would naturally come from the Olympic Store’s business.

To denote a contained relationship that effectively hides a contained class from “outsiders”.

Using a container can simplify a model; the container will be a proxy for all attributes and operations of the contained class. The contained class can be hidden.

A composite relationship is reflected in code; if it is not specified then the code may not do what it is meant to. (e.g. the contained class may be visible to “outsiders”).