First, I should describe what are 1-to-0..* and 1-to-1..*. 1-to-0..* means 1 object of this class can have relationship with 0 or more objects of another class. And 1-to-1..* means 1 object of this class can have relationship with 1 or more objects of another class.
A tourist guide can guide zero or more tours. But an order, it must contains at least one order item. In contrary, a tour and an order item cannot exist without tourist guide and order.
Of course, in TouristGuide class and Order class should contains a collection to keep the tours and order items. But how to represent the 1-to-0..* and 1-to-1..* relationships? User can call getTours() or getOrderItems() any time he wants.
I think in 1-to-0..* relationships, when user invokes getTours() on a TouristGuide, he can have a collection contains no element if that TouristGuide doesn't have any Tour to guide. But in 1-to-1..* relationthips, when user invokes getOrderItems() on an Order, he can have a null value if that Order didn't contain any OrderItem yet.
class TouristGuide {
private ArrayList<Tour> tours = new ArrayList<Tour>();
public ArrayList<Tour> getTours() {
return tours;
}
public void addTour(Tour tour) {
tours.add(tour);
}
...
}
class Order {
private ArrayList<OrderItem> orderItems;
public ArrayList<OrderItem> getOrderItems() {
return orderItems;
}
public void addOrderItem(OrderItem orderItem) {
if (orderItems == null) {
orderItems = new ArrayList<OrderItem>();
}
orderItems.add(orderItem);
}
...
}
No comments:
Post a Comment