1

I am searching for a design solution to the problem where I have 2 classes which depend on each other such that I have class Customer and class Order where:

Customer can have a list of orders (1-to-N) and an Order has a designated customer (1-to-1).

What is the best practice to break these kind of dependencies?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Koray
  • 467
  • 1
  • 5
  • 12
  • This is a problem I occasionally come up with and although there are a couple of articles on the internet on solving cyclic dependencies I am unable to come up with the best solution so far for this particular case. I would appreciate if you have any suggestions. – Koray Feb 13 '16 at 16:30

2 Answers2

3

Assuming you have a dependency as follows:

public class Customer {
    private long customerId;
    private String name;
    private String address1;
    // ....
    private List<Order> orders;
}

public class Order {
    private long orderNumber;
    private Date orderDate;
    // ... others
    private Customer customer;
}

You could create a third class to break the dependency:

public class CustomerOrder {
    private final Customer customer;
    private final List<Order> orders;
    public CustomerOrder(Customer customer) {
        super();
        this.customer = customer;
        this.orders = new ArrayList<Order>();
    }
    public void addOrder(Order order) {
        orders.add(order);
    }
    public Customer getCustomer() {
        return customer;
    }
    public List<Order> getOrders() {
        return orders;
    }
}

Now you can drop orders from the Customer class, and customer from the Order class. Or am I misunderstanding your issue?

Ian Mc
  • 5,656
  • 4
  • 18
  • 25
  • Thanks - that's the issue yes. My question would be then, if I want to update a specific order I need to find the appropriate `CustomerOrder` object which has this order in it. So I need to do a search on all CustomerOrders to go through all the orders in each of them to find the correct CustomerOrder object, then update the order. Is there a way to overcome this expensive 'find-and-update' operation too? – Koray Feb 13 '16 at 17:22
  • The find need not be expensive. You would have to create an index on orders for quick lookup by order number. Is this lookup done using a database? – Ian Mc Feb 13 '16 at 17:36
  • No, not a database at this stage. Could you please elaborate with an example for the lookup of an order in that case? I am still struggling to understand/find an efficient way to do it. – Koray Feb 13 '16 at 17:43
  • One way (maybe not the best) is: Assume you have 'int orderNumber'. Now create a HashMap where the key is the orderNumber. Everytime you add a customerOrder, you add entries into the Map (one for each order). Now you have very fast retrieval time: Map.get(orderNumber) retrieves the customerOrder – Ian Mc Feb 13 '16 at 17:47
  • That looks like the most appropriate way for this case - thanks – Koray Feb 14 '16 at 00:22
-1

As a software engineer for approx 2 years, the best I’ve seen for a case like that is to put a shadow definition of one class, without initializing it to anything, simply telling the compiler “hey orders exist”, then defining the other class explicitly, followed by your orders class explicitly. Does that get you in the right direction? Nodes and trees sometimes are modeled this way, and data structure and analysis of algorithms books tend to have decent design solutions to this too.

bmc
  • 817
  • 1
  • 12
  • 23