Class and component dependency cycles
Assume, we have component A
with class CA
, and component B
with class CB
, with a cyclic two-way dependency between classes CA <--> CB
. Hence, we have a cyclic dependency between components A
and B
.
Resolving class dependency cycle
A standard way to resolve the class cycle is to introduce interfaces (e.g. IA
in A
and IB
in B
) and let the classes depend on the interfaces. Now we have IA <|-- A --> IB
and IB <|-- B --> IA
with no cyclic dependency between the classes, but still between components.
Resolving component dependency cycle
On frequently suggested idea (e.g. by Robert Martin) is to move one of the interfaces to the other component. This does resolve the cyclic dependency between components.
Issues with the approach
For somebody who is used to have interface and class in the same component, this feels unnatural because they seem to belong together. Probably I am thinking too much in terms of what services a class is offering and then extracting the interface instead of what services consumers require and let the implementation be completely independent in my mind.
Question
Any ideas how I can even better justify moving an interface IA
to component B
when implementation CA
is remaining in component A
?
dn1h is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.