How can I improve the design of the code below:
class Foo {
public Configuration configure() {
return new Configuration().withPropertyA().withPropertyB();
}
}
class Configuration{
private boolean propertyA = false;
private boolean propertyB = false;
public Configuration withPropertyA() {
this.propertyA = true;
return this;
}
public Configuration withPropertyB() {
this.propertyB = true;
return this;
}
public boolean hasPropertyA() {
return this.propertyA;
}
public boolean hasPropertyB() {
return this.propertyB;
}
...
}
class Client{
public void someMethod(Configuration config) {
if (config.hasPropertyA() {
doStuffA();
}
if (config.hasPropertyB() {
doStuffB();
}
//...
}
}
I.e., the Configuration class holds some flags for the caller (Client
) that tell to the caller which "things" need to be configured. The Client knows what to do for each of the flags if they are set. I want to get rid of the if statement in Client
as well as of the concept of simply setting boolean flags in the Configuration
instance. Can you suggest a more "generic" and object oriented approach to this?
kind regards