Java Object with optional fields -


i have 1 scenario , please have look

sentence can :

string subject + string verb + string object

string subject + string verb + string adverb+ string object

string subject + string verb + string adjective+ string object

string subject + string verb

i want create sentence class object.

  sentence s = new sentence();     s.setsubject();     ... 

now , can keep fields in sentence class, i want ensure no field must null. want create type of sentence fields i initialize create object.

eg:  sentence s = new sentence(); s.setsubject(); s.setverb(); s.setobject(); s.getvalue(); // return sentence value s+v+o have set svo  sentence s = new sentence(); s.setsubject(); s.setverb(); s.getvalue(); // return sentence value s+v have set sv 

and on ... can in java.

basically, i want avoid uninitialized fields , nullpointers.

edit

please note if leave fields empty ie "" or null , sentences formed

"i null school." or (verb null) .

"i am to school" (verb empty) .

so, please consider scenario before answering.

i prefer using builder pattern, make usage clearer end user , avoid mistakes

    public class sentence {          private string subject;         private string verb;         private string adjective;         private string object;      //package private ensure no 1 can call outside package, canbe made pvt         sentence(string subject, string verb, string adjective, string object) {             this.subject = subject;             this.verb = verb;             this.adjective = adjective;             this.object = object;         }          public static class sentencebuilder{             private string subject;             private string verb;             private string adjective;             private string object;              private static final string empty = "";              private string sanitizeinput(string input){                 if (input==null){                     return empty;                 }                 return input;             }              private string validateinput(string input){                 if(input==null || input.isempty()){                     throw new illegalargumentexception("cant null or empty");                  }                 return input;             }              public sentencebuilder(string subject, string verb) {                 this.subject = validateinput(subject);                 this.verb = validateinput(verb);             }              public sentencebuilder adjective(string adjective){                 this.adjective = sanitizeinput(adjective);                 return this;             }              public sentencebuilder object(string object){                 this.object = sanitizeinput(object);                 return this;             }              public sentence build(){                 return new sentence(subject,verb,adjective,object);             }         }          public static void main(string[] args) { //sample usage             sentence sentence = new sentencebuilder("subject","verb")                     .adjective("adjective")                     .object("object")                     .build();         }     } 

Comments