Sobes.tech
Junior — Middle

What type of Builder pattern is used when the result of its execution does not matter?

sobes.tech AI

Answer from AI

When the construction of an object does not matter, the Builder pattern with Fluent Interface method is used, where methods return the builder itself, allowing method chaining for configuration, but the final object may not be used or needed. This approach is often used for configuration or setup where the process of setting parameters is important, not the creation of the final object.

Example in Kotlin for Android:

class LoggerBuilder {
    private var level: String = "INFO"
    private var tag: String = "App"

    fun setLevel(level: String) = apply { this.level = level }
    fun setTag(tag: String) = apply { this.tag = tag }

    fun build() {
        // No object returned, just configure the logger
        println("Logger configured with level=$level and tag=$tag")
    }
}

// Usage:
LoggerBuilder()
    .setLevel("DEBUG")
    .setTag("MyApp")
    .build()  // The result is not important, the process of setup is