There are various types of circular dependencies:
- instantiation dependency - object A includes object B which in turn includes object A, hence in order to create object A you need to have in hand object B, but in order to create object B you need to have object A...
- operational dependency
- object A invokes methods on object B and object B invokes methods on object A
- classes from package com.abc depend on classes from package com.xyz, while classes from com.xyz depend on classes from com.abc
In case of instantiation dependency it is impossible to create neither objects A nor B. In case of operational dependency it might be hard to understand the operational flow and responsibilities of given module/package.
So how to solve it?
First of all let's assume that we need to keep the relation between two objects but we also need to solve the circular part of it. Let's use following classes as an example:
class Customer {
List<Order> orders
}
class Order {
Customer owner
}
There are conceptually three ways we can undertake:
This solution requires deep understanding of the business as it indicates that what we considered as separate concepts should/could actually be modeled as one.
Brak komentarzy:
Prześlij komentarz