Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design for intreraction between two unrelated modules.

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application there are two unrelated modules.

Now there is a scenario in which I want to send some notification from one module to another if some particular condition is met.

Using messaging service it is easy but we are not using it in our application.

How this can be implemted in java is there any particular API through which it can be achieved?

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends. Do they run on the same machine? The same JVM? One way which I like is to open a socket connection between the two in the case they run on different JVMs.
 
sourabh girdhar
Ranch Hand
Posts: 71
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use socket as suggested by John.

You can also use plain java RMI. quite easy and simple method.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is on the same JVM.

Suppose there are two modules A and B and I want to send notification to B from A.

one way is to get the reference of B in A and invoke B's method but that is not the right
approach because i want to keep it generic and do not want to add any dependecies and more over
module B acts as pluggable component which may or may not be there.

Is there other approach for this or any design pattern which can be helpful?



 
Stephan van Hulst
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Publisher/subscriber.

Let A have a register method that accepts some sort of subscriber interface S (also known as a listener). A will keep track of a list of these subscribers.
When a change happens, A will then call the appropriate method in S of each subscriber.

B simply implements S, or has a helper class (tip: anonymous local class) that implements S and registers itself (or it's helper class) through A's register method.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see the Observer Pattern.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic