Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package equivalent visibility modifier in Kotlin

Tags:

java

kotlin

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.

like image 355
LordScone Avatar asked Feb 22 '16 09:02

LordScone


1 Answers

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.

like image 195
yole Avatar answered Oct 28 '22 02:10

yole