Builder Pattern Config - Problem
Implement a Configuration Builder using the Builder Pattern with a fluent API. The builder should create complex configuration objects with various optional parameters.
Your task is to create a ConfigBuilder class that allows method chaining to set different configuration options. The builder should support setting server host, port, database name, connection timeout, retry attempts, and enable/disable SSL.
Implement the following methods:
setHost(host)- Set server hostsetPort(port)- Set server portsetDatabase(dbName)- Set database namesetTimeout(seconds)- Set connection timeoutsetRetries(count)- Set retry attemptsenableSSL()- Enable SSL connectionbuild()- Create final configuration object
The final configuration should be returned as a formatted string containing all set values.
Input & Output
Example 1 — Basic Configuration
$
Input:
operations = [["setHost", "localhost"], ["setPort", 8080], ["build"]]
›
Output:
Config(host=localhost, port=8080)
💡 Note:
Builder sets host to localhost, port to 8080, then builds the configuration string
Example 2 — Full Configuration
$
Input:
operations = [["setHost", "server.com"], ["setPort", 3306], ["setDatabase", "mydb"], ["setTimeout", 30], ["enableSSL"], ["build"]]
›
Output:
Config(host=server.com, port=3306, database=mydb, timeout=30, ssl=true)
💡 Note:
Builder chains multiple method calls to create a comprehensive configuration
Example 3 — Minimal Configuration
$
Input:
operations = [["setDatabase", "testdb"], ["build"]]
›
Output:
Config(database=testdb)
💡 Note:
Builder with only database parameter set, demonstrating optional parameter handling
Constraints
- 1 ≤ operations.length ≤ 10
- Each operation is either a setter method with parameter or build()
- String parameters have length ≤ 100
- Integer parameters are in range [1, 10000]
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code