https://kotlinlang.org logo
Title
m

Mani

10/11/2019, 7:38 PM
Trying to implement strategy pattern. Let me know if this is bad code
p

Pavlo Liapota

10/11/2019, 8:04 PM
What error? Works fine for me https://pl.kotl.in/gZPnK0byh Just write
object
instead of
Object
.
m

Mani

10/11/2019, 8:04 PM
Also, is this correct?
val labelByCategoryMap = mapOf(
            ShippingLabelCategoryConstants.FRESH to FreshLabelPrinter,
            ShippingLabelCategoryConstants.PHARMA to PharmaLabelPrinter
    )
where
FreshLabelPrinter, PharmaLabelPrinter
are objects
Can I have map of enum to objects?
p

Pavlo Liapota

10/11/2019, 8:06 PM
Sure
Do they implement same interface? Otherwise this may not be very useful.
m

Mani

10/11/2019, 8:08 PM
yeah they do. Let me share the exact scrensot
p

Pavlo Liapota

10/11/2019, 8:16 PM
FreshLabelPrinter
does not implement any interface, so I can guess that your map
labelByCategoryMap
will have type
Map<ShippingLabelCategoryConstants, Any>
, that’s why method is not available.
m

Mani

10/11/2019, 8:18 PM
Please suggest me a better alternative implementation
I don't want to duplicate the code and make it readable instead of bunch of functions together.
p

Pavlo Liapota

10/11/2019, 8:31 PM
If I understood your use-case correctly, then this may help https://pl.kotl.in/HMdOjuGHP
m

Mani

10/11/2019, 8:32 PM
Thank you so much! It looks the interface is missing. 🙂
👍 1