I have a class which is there for you to specify all possible parameters for a URL, but you don't have to.
Given is e.g. the following class:
class Foo {
private String a;
private String b;
private String c;
}
Now it is possible that not all parameters were set for an instance of a class and thus some are null. Up to here no problem. Now I want to create a string from this class, which contains only all non-null parameters.
e.g:
Foo foo = new Foo();
foo.setA("a");
foo.setC("c");
You could just do it with if else queries, but it just doesn't seem right to me. It has something of a bad style.
At all if there are many parameters and I have to adjust the code permanently when I add or remove parameters. Does anyone have an idea how to solve it differently.
Many thanks in advance.
EDIT
Sorry for the confusion, the current structure looks like this:
interface Request {
String getUrl();
}
class A implements Request {
...
public String getUrl() {
...
}
}
class B implements Request {
...
public String getUrl() {
...
}
}
...
And in this example, there may be several classes that implement the interface. Where each class has different attributes. Some must be present, others not. And the method getUrl() returns the url of the class parameters based on the parameters, which are not null.