parent
d572f0717c
commit
e196c5494d
@ -0,0 +1,134 @@
|
||||
/**
|
||||
* Product class: Complex configuration object
|
||||
* Contains required and optional parameters, created via builder pattern
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
/**
|
||||
* Builder class for Config
|
||||
*/
|
||||
public static class ConfigBuilder {
|
||||
|
||||
/**
|
||||
* Default encoding UTF-8
|
||||
*/
|
||||
private String encoding = "UTF-8";
|
||||
/**
|
||||
* Default max connections 10
|
||||
*/
|
||||
private int maxConnections = 10;
|
||||
/**
|
||||
* Default timeout 3000ms
|
||||
*/
|
||||
private int timeout = 3000;
|
||||
/**
|
||||
* Required parameter (passed in builder constructor)
|
||||
*/
|
||||
private final String url;
|
||||
|
||||
/**
|
||||
* Builder constructor: requires mandatory parameter
|
||||
*
|
||||
* @param url Target service URL (required)
|
||||
*/
|
||||
public ConfigBuilder(String url){
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Config object and validate parameter legality
|
||||
* @return Immutable Config instance
|
||||
*/
|
||||
public Config build(){
|
||||
// Validate parameters
|
||||
if (url == null || url.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("URL cannot be null or empty");
|
||||
}
|
||||
if (maxConnections <= 0) {
|
||||
throw new IllegalArgumentException("Max connections must be positive");
|
||||
}
|
||||
if (timeout <= 0) {
|
||||
throw new IllegalArgumentException("Timeout must be positive");
|
||||
}
|
||||
return new Config(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set encoding format (optional parameter)
|
||||
* @return Builder itself (for method chaining)
|
||||
*
|
||||
* @param encoding Encoding format (e.g. "GBK")
|
||||
*/
|
||||
public ConfigBuilder setEncoding(String encoding){
|
||||
this.encoding = encoding;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set max connections (optional parameter)
|
||||
* @return Builder itself (for method chaining)
|
||||
*
|
||||
* @param maxConnections Max connections
|
||||
*/
|
||||
public ConfigBuilder setMaxConnections(int maxConnections){
|
||||
this.maxConnections = maxConnections;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set timeout (optional parameter)
|
||||
* @return Builder itself (for method chaining)
|
||||
*
|
||||
* @param timeout Timeout in milliseconds
|
||||
*/
|
||||
public ConfigBuilder setTimeout(int timeout){
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional parameter, demonstrating extensibility
|
||||
*/
|
||||
private final String encoding;
|
||||
private final int maxConnections;
|
||||
/**
|
||||
* Optional parameter (has default value, can be modified via builder)
|
||||
*/
|
||||
private final int timeout;
|
||||
/**
|
||||
* Required parameter (must be set, no default value)
|
||||
*/
|
||||
private final String url;
|
||||
|
||||
/**
|
||||
* Private constructor: only allowed to be called by internal builder
|
||||
*
|
||||
* @param builder Builder object providing parameters
|
||||
*/
|
||||
private Config(ConfigBuilder builder){
|
||||
this.url = builder.url;
|
||||
this.encoding = builder.encoding;
|
||||
this.maxConnections = builder.maxConnections;
|
||||
this.timeout = builder.timeout;
|
||||
}
|
||||
|
||||
public String getEncoding(){
|
||||
return encoding;
|
||||
}
|
||||
|
||||
public int getMaxConnections(){
|
||||
return maxConnections;
|
||||
}
|
||||
|
||||
public int getTimeout(){
|
||||
return timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter methods for all parameters (no setters to ensure immutability)
|
||||
*/
|
||||
public String getUrl(){
|
||||
return url;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue