2

In my program; I have an object which stores name, description, languages that he/she speaks, when created, address, reputation. But the must have fields are name, description, languages and the others are non compulsory. I don't want to write two methods and let user select one and the make other fields null according to usage, such as;

inside of constructor(name, description, languages) {
    this.name = name;
    this.description = description;
    this.languages = languages;
    this.address = " ";
    this.reputation = " ";
}

inside of constructor(name, description, languages, address, reputation) {
    this.name = name;
    this.description = description;
    this.languages = languages;
    this.address = address;
    this.reputation = reputation;
}

So how can I solve this problem? Is there a way I can create fields according fields that user provides?

I am using java.

Thanks in advance.

maba
  • 47,113
  • 10
  • 108
  • 118
yns
  • 440
  • 2
  • 8
  • 28
  • Is there a special reason why you don't want two constructors? – Fildor Sep 21 '12 at 07:50
  • @Fildor The first reason is I don't want to create redundant fields and make them null. And the other reason is I don't think these types of codes are beatiful – yns Sep 21 '12 at 08:06
  • So the optional fields are by far more often not used than they are? No offense, but I have the feeling, that this is a little bit overkill. Maybe a question of personal taste. One hint though: have you thought about the decorator pattern? http://www.oodesign.com/decorator-pattern.html – Fildor Sep 21 '12 at 10:17

6 Answers6

1

A better option would be to provide getter setter methods for the variables, so that the user can call only those methods that set values for mandatory fields. So try creating a Bean class mapping to the form, with fields mapping to the variables.

1

Have a look at the following URL which explains Builder Pattern.

https://stackoverflow.com/a/1953567/705315

~HTH

PS : Unable to comment on your question. Hence posting as answer.

Community
  • 1
  • 1
ranjit
  • 158
  • 7
0

To have a different set of fields you need a different class. You could generate a class for every combination if you wish using a factory or builder to do this (I have done this before and its not simple ;) but I would create a one class which has all possible fields and use that.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

There is no "default value" for parameters in Java, so you would have to provide 2 constructors (one can be calling the other though).

If your fields are quite dynamic, what about using a Map<String, String> as a single parameter? This is usually not a pretty pattern but it could fit your use case.

mauhiz
  • 506
  • 5
  • 14
0

You can make a constructor with a variable number of arguments:

public class VarArg {
    public VarArg(String ... args)
    {
        String a = args[0];
        String b = args[1];
    }

    public static void main(String[] args)
    {
        VarArg arg1 = new VarArg("hello");
        VarArg arg2 = new VarArg("hello","goodbye");
    }
}

But I would probably use a Map with named keys as the parameter to your constructor (as suggested by mauhiz above).

Luhar
  • 1,859
  • 2
  • 16
  • 23
0

The java compiler API allows you to invoke the Java compiler with source code that you can specify at runtime in another java program. Probably not what you are looking for, but it is possible to create a class with fields the user specifies at runtime to answer your question. I can provide an example if you want.

xshoppyx
  • 1,444
  • 1
  • 8
  • 9