domain model—part 6: special associations - composition bts430 systems analysis and design using...

14
DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Upload: agatha-hicks

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

DOMAIN MODEL—PART 6:SPECIAL ASSOCIATIONS -

COMPOSITION

BTS430 Systems Analysis and Design using UML

Page 2: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Let’s look at an Order

Product

productIDproductNameingredientSet : Ingredientcategory : Category

OrderLine

product : Productquantity

11..* 11..*

orders quantity of

Order

orderIDorderDateorderTotalcustomer : CustomerproductOrderedSet : OrderLine

1..*

1

1..*

1

contains

Page 3: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Order and OrderLine

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.

Page 4: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Order and 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.

Page 5: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

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)

Page 6: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

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

Page 7: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

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)

Page 8: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

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.

Page 9: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Composition

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.

Page 10: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

The Composition Symbol

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.

Page 11: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

The Composition Symbol

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

Page 12: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

A Note About Aggregation

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 BTS430. It has limited use in modeling and has no definite “meaning” when translated to code.

Page 13: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Composition in BTS430

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

It is easier because those are the relationships that would naturally come from the Harbourside Cycling business.

Page 14: DOMAIN MODEL—PART 6: SPECIAL ASSOCIATIONS - COMPOSITION BTS430 Systems Analysis and Design using UML

Why Use Composition?

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”).