I\'m struggling to come up with a good way of adding a bidirectional relation in OO model. Let\'s say there is a Customer who can place many Orders, that is to say there is a on
I think the best way in this case is to delegate the responsibility for wiring to another class:
class OrderManager {
void placeOrder(Customer c, Order o){
c.addOrder(o);
o.setCustomer(c);
}
}
class Customer {
private Set orders = new LinkedHashSet();
void addOrder(Order o){ orders.add(o); }
}
class Order {
private Customer customer;
void setCustomer(Customer c){ this.customer=c; }
}