2

I am working on existing code to enhance the functionality.

In existing code I saw there are multiple constructors with string parameters. like

public class A {
    public A(String a){
    }

    public A(String a, String b){
    }

    public A(String a, String b, String c){
    }
}

While enhancement i found that i need to add another string parameter to the constructor. However this seems to be problem, there could be another enhancement in that i have to add anther string.

I want to avoid such scenario.

What could be the best design to avoid such scenario?

Is good solution is to use HashMap?

6
  • Is this about Java? (You haven't specified the language in your question. I'm only inferring based on the mention of HashMap.) Commented Aug 19, 2012 at 20:39
  • Yes you can say this is about java. but this issue can arise in any language. Commented Aug 19, 2012 at 20:43
  • I've added the tag. For future questions, please be more specific in your question to start with - very often different languages encourage different design idioms. Commented Aug 19, 2012 at 20:44
  • Can't you just keep a Collection of Strings as an instance variable? Without telling us what these Strings represent, we can only guess. Commented Aug 19, 2012 at 21:14
  • @maenu didn't get your question. Commented Aug 19, 2012 at 21:28

2 Answers 2

2

Is good solution is to use HashMap?

It's not really clear what problem you're trying to solve, but you might want to consider encapsulating the parameters in another class - perhaps one which is mutable, following the builder pattern. That would leave you with something like:

FooParams params = new FooParams().withA("a value")
                                  .withB("b value")
                                  .withC("c value");
Foo foo = new Foo(params);

(You could even have a buildFoo() method in FooParams to keep the whole thing fluent.)

Sign up to request clarification or add additional context in comments.

Comments

2

Try with a solution that uses varargs.

public class A {
    public A(String ... args) {
        // Add some logic that validates number of input parameters etc.

        for (String arg : args) {
            ...
        }
    }
}

You can call it like this:

A a1 = new A("tagA: value1", "tagB: value2", "tagC: value3");
A a2 = new A();
A a3 = new A("<tagA>234</tagA>");

2 Comments

This could work, but then the argument order would still need to be fixed & known ahead of time. I think the issue becomes that if each arg is a String, it's not always obvious what that order is. I like the Builder pattern idea a bit more, if only because it simulates named arguments.
@DougSwain Agree on that. But since the OP hasn't told us what his class is for then maybe this is a valid solution. Maybe he wants to take just a bunch of strings and manipulate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.