1

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.

EraZer
  • 75
  • 6
  • Is there a reason to not use a Map instead of having separate variables for each potential parameter? – Tim Sep 06 '22 at 17:03
  • Could work. It's just that there is not only Foo but several classes which implement an interface which I then access. And each class (like Foo) specifies certain properties. – EraZer Sep 06 '22 at 17:08
  • What do you mean by "create a string from this class"? Like `foo.toString()`? – eternal Sep 06 '22 at 17:27
  • Kinda. But the "problem" is how to return the string, rather how to do it nice and clean ^^ – EraZer Sep 07 '22 at 12:10

1 Answers1

1

If you cannot move different properties of class in HashTable or Map, then you can use reflection to iterate through attributes properties of class.

However, if you can move properties into HashTable or Map then we can use the following code snippets. I am sorry, I am not Java guy, but I've made examples via C# and wrote comments how it can be implented via Java.

If getUrl has different implementations, then we can define implementation in each class like this:

interface IFooRequest
{
    string getUrl();

    Dictionary<string, string> GetParameters();
}

And its concrete implemetations:

class A : IFooRequest // "implements"  in Java
{
    public Dictionary<string, string> GetParameters()
    {
        return new Dictionary<string, string> 
        {
            { "a", "foo"}
        };
    }

    public string getUrl()
    {
        throw new NotImplementedException();
    }
}

class B : IFooRequest // "implements"  in Java
{
    public Dictionary<string, string> GetParameters()
    {
        return new Dictionary<string, string>
        {
            { "b", "bar"}
        };
    }

    public string getUrl()
    {
        throw new NotImplementedException();
    }
}

If getUrl has the same implementation, then we can define abstract class and reuse its behaviour:

public abstract class FooRequest
{
    public string GetUrl() { return ""; }

    public abstract Dictionary<string, string> GetParameters();
}

class A : FooRequest // "extends" in Java
{
    public override Dictionary<string, string> GetParameters()
    {
        return new Dictionary<string, string>
        {
            { "a", "foo"}
        };
    }
}

class B : FooRequest // "extends" in Java
{

    public override Dictionary<string, string> GetParameters()
    {
        return new Dictionary<string, string>
        {
            { "a", "foo"}
        };
    }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148