https://kotlinlang.org logo
#naming
Title
# naming
k

Kshitij Patil

03/11/2023, 1:54 PM
What should be the name for a class which builds something but is not really implementing
Builder
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.
m

mbonnin

03/11/2023, 2:07 PM
You could top level builder functions:
Copy code
buildMyNotification {
  title = "Hello World"
  timeout = 10.seconds
}
or
Copy code
MyNotification(
  params = NotificationParams(
    title = "Hello World"
    timeout = 10.secons
  )
)
j

Javier

03/11/2023, 2:23 PM
operator fun invoke
on the
interface
is another option
Copy code
interface Foo {
  
    companion object {

         operator fun invoke(param: FooParam) = object : Foo {
             ...
         }
    }
}

// Consumer
fun main() {
  val foo = Foo(param = FooParam(...))
}
k

Kshitij Patil

03/12/2023, 6:40 AM
This can’t be a function. I want to have multiple subclasses implementing this. Like there will be Alarm Notification Builder, Sticky Notification Builder and I want to have proper DI setup for all of these. The concern is not how to implement it, but what to name this class. Let’s say it’s building
Notification
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
)
k

kqr

03/12/2023, 6:26 PM
Factory?
d

Dmitry Khalanskiy [JB]

03/12/2023, 9:36 PM
I still don't get why it can't be a function. There can be multiple functions of the same type.
l

louiscad

03/13/2023, 2:44 PM
Are you working on something that looks like this?
m

Marcello Galhardo

03/19/2023, 1:19 AM
From your first message I would say you are describing an oldie but goodie Factory - so that is the name I would go for. That said, your second message made me suspect you may be trying to implement a variation of Abstract Factory (?). Maybe you want to check this: https://refactoring.guru/design-patterns/abstract-factory
5 Views