6
votes

I'm writing a program in which I'm using the factory pattern. I have an interface called AssetFundCalculator and a class named AssetFundCalculatorImpl which implements the interface.

The AssetFundValidator class validates some inputs, and is able to instantiate a valid AssetFundCalculatorImpl.

I'd like if the rest of the program would only know about the interface, and not about the AssetFundCalculatorImpl, so that only the AssetFundValidator could instantiate it.

My package structure is looking like this:

enter image description here

In Java, I would give package visibility modifier to the AssetFundCalculatorImpl class, or it's constructor, and it would solve the issue, but in Kotlin, there is no package visibility modifier.

Is there a solution to this? I don't want to put this code to a separately compiled module, so the internal modifier is not going to work.

1

1 Answers

4
votes

You can put AssetFundValidator and AssetFundCalculatorImpl into the same file and mark AssetFundCalculatorImpl as private. In that case it will only be accessible to code in the same file, including AssertFundValidator, but not to any code in other parts of the program.