Trying to implement strategy pattern. Let me know ...
# announcements
m
Trying to implement strategy pattern. Let me know if this is bad code
p
What error? Works fine for me https://pl.kotl.in/gZPnK0byh Just write
object
instead of
Object
.
m
Also, is this correct?
Copy code
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
Sure
Do they implement same interface? Otherwise this may not be very useful.
m
yeah they do. Let me share the exact scrensot
Screen Shot 2019-10-12 at 1.39.39 AM.png
p
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
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
If I understood your use-case correctly, then this may help https://pl.kotl.in/HMdOjuGHP
m
Thank you so much! It looks the interface is missing. 🙂
👍 1