This is a slightly abstract question about finding a pretty design approach with minimal boilerplate.
Prerequisites:
enum class Provider { Google, Microsoft }
interface Foo { fun getMail(): Mail }
that will be implemented for each specific provider. I was curious if there is a way to define ENUM class Provider
in such way that it's implementing interface Foo
and I can later specify by which objects each concrete provider will be implemented?
I wonder if there can be a boilerplate-less way to define enum class of concrete interface while I can later define by which objects concrete provider will be implemented.
Prerequisites aren't solid so if a better design requires changes then I'm eager for a better suggestion.
Yep
You can make the enum implements the interface.
enum class Provider(val mail: Mail) : Foo {
Google(googleMail),
Microsoft(microsoftMail);
override fun getMail(): Mail = mail // Or this.mail
}
interface Foo { fun getMail(): Mail }
Then you access
Provider.Google.getMail()
Other way is using val members
interface Foo { val mail: Mail }
enum class Provider(override val mail: Mail) : Foo {
Google(googleMail),
Microsoft(microsoftMail)
}
And access
Provider.Google.mail
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.