java - Circular dependency with constructor injection -


say have following components:

enter image description here

  • producer produces numbers , sends messages consumer
  • both producer , consumer send messages monitor
  • monitor, randomly, decides when produce / consume process should stop , sends message stopper
  • stopper stops both producer , consumer cleanly

i know easy accomplish in mutable language such java. know can resolved allowing partial mutability interfaces, such described here.

however, it's not practice have cyclic dependencies if possible. so, let's assume references constructor-injected , final:

  • producer has final consumer , final monitor
  • consumer has final monitor
  • monitor has final stopper
  • stopper has final producer , final consumer

i found references such this, don't seem apply.

how 1 go un-cycling case , cases such in general? in other words, i'm interested in how accomplish not forming cycles design standpoint. hints?

you're right, won't work if dependencies final , injected via constructor.

but may ask, why have injected via constructor? there nothing wrong @ end of day use setters wire beans.

in fact, in spring, beans instantiated first , injected afterwards. @ approach.

other that, @ different way model problem (that not have circular dependencies).

for example, since using queues send messages between producer , consumer, why not send messages on queues monitor? stopper send messages producer , consumer.

or, taylor suggests, esb.

there many other ways design it, have read (for example) apache camel enterprise integration patterns ideas.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -