-2

I'm working on a Gradle project that has several modules. The project is implemented in Kotlin. One of these modules is the main entry point to the project, which is the main module. Another module provides a number of utilities to the main module.

My problem is that these utilities module requires some command line arguments. These command line arguments are only available in the main module.

My question is: what would be the best way to pass the data from the main module to the utilities module? The key part is that, these command line arguments initialise an object which is provided through some external library. When the command line arguments are read, an object of this external library is initialised.

I have tried/considered several options:

  1. Modifying constructors and function definition in both modules to accommodate this new object.
  2. Create a singleton from this object.
  3. Create a singleton from this object and wrap in a service. Then use the service locator pattern to find a service.

I think the problem with the last two approaches is that the data which are the command line arguments needs to be saved and retrieved when the singleton object needs to be re-initialised.

TL;DR How do I pass command line arguments from one part of the project to another without modifying constructors or function definitions?

Related I think the posts below explain better what I'm asking in this question, but I'm wondering if there is anything in Java/Kotlin that provides this functionality out of the box, or if there are some examples how to do this.

  1. How to pass command line parameters to various parts of program

  2. How to pass data between objects when an IoC container is being used?

2
  • There's something I don't understand. You have MainModule and some external modules (em1, em2...). You want to pass datafrom MainModule to em1. em1 require command line arguments that are not the data you want to pass. Is that it?
    – JayZ
    Jun 3 '20 at 11:46
  • Would like to pass some command line arguments to em1. I don't want to modify constructors and functions definitions to get into em1. Hope this makes sense
    – Andrei
    Jun 3 '20 at 11:50
1

TL;DR How do I pass command line arguments from one part of the project to another without modifying constructors or function definitions?

If a module is called by command line then you have to treat this like any interface. You only have two choices:

  • Adhere to it
  • Create a new interface

If you can't create a new interface (a new constructor or command line call) and the one provided does not match your expectations you have to step back a bit.

Do you really need to pass those data to your module ?

Think back about what you're trying to achieve. Do you really need to pass the data you want to this module?

Can you transpose your data to a structure expected by the module ?

If the module is not expecting what you're trying to achieve there is only two reasons to this:

  • You're trying to do something not expected by the module and thus you have to find another way to achieve your goal
  • Expectations have changed and the module need to evolve to match the new expectation.
0

For what I understand, the main module gets the command line arguments (CLA), and the consumer module creates an object with them. But you need to share them, preferably without changing the signature.

I would always change the signature, or an object passed in it, if that's possible. But if that would imply making a load of changes and impacting other modules, I would use a singleton. You should assess the advantages and costs of each solution. The solution follows not a rule, it depends on multiple factors that only you you can evaluate.

And no, AFAIK, there's no native way of sharing atomic properties. But that's not a Kotlin issue. That's a JVM issue and a traditional problem, mostly easy to solve according to the context.

A service locator pattern seems like killing an ant with a cannonball.

1
  • Changing the signature is possible, and it is the solution that will be preferred if none others work. Using a singleton is a valid approach and one that I considered, but it is criticized in many posts as being a code smell (see stackoverflow.com/questions/137975/…).
    – Andrei
    Jun 9 '20 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.