Kshitij Patil
03/11/2023, 1:54 PMBuilder
pattern. You simply have one method Foo.build(param)
in the class interface which gives you the instance. Such classes use some other builder hierarchy by feeding received configurations in param
and gives you concrete instance, IE, abstracting away builder logic.
For instance, I have a custom notification builder class which is using NotificationCompat.Builder()
internally and giving Notification
instance on invocation.mbonnin
03/11/2023, 2:07 PMbuildMyNotification {
title = "Hello World"
timeout = 10.seconds
}
or
MyNotification(
params = NotificationParams(
title = "Hello World"
timeout = 10.secons
)
)
Javier
03/11/2023, 2:23 PMoperator fun invoke
on the interface
is another optioninterface Foo {
companion object {
operator fun invoke(param: FooParam) = object : Foo {
...
}
}
}
// Consumer
fun main() {
val foo = Foo(param = FooParam(...))
}
Kshitij Patil
03/12/2023, 6:40 AMNotification
object, I don’t really want to name it MyNotificationBuilder
, because I’m not implementing builder pattern and the name might give wrong ideas. Some of the options I came up with
• MyNotificationCreator
• MyNotificationComposer
• MyNotificationFactory (to some extent we can call it a factory because I’ll be having multiple subclasses creating their own variant of Notification
)kqr
03/12/2023, 6:26 PMDmitry Khalanskiy [JB]
03/12/2023, 9:36 PMlouiscad
03/13/2023, 2:44 PMMarcello Galhardo
03/19/2023, 1:19 AM