mardi 28 février 2017

Construct object in separate methods

I have a POJO with three properties- two separate methods are required to get values for two properties out of these three properties. I want to use builder patterns to construct the object.

 class Main {
      createObjectOfPOJO {
           //use builder pattern
      }

      methodToGetValueOfType1(){..}

      methodToGetValueOfType2(){..}
 }

 class POJO {
      String simpleProp;
      List type1;
      List type2;
 }

Main class contains two separate methods to get values of type1 and type2 list.

If I use builder pattern, I can construct object in first method where I will supply value of type1 and simpleProp. Then I need to call second method to get value of type2.

Is it possible to use builder pattern to create object of POJO using these two methods?

I have currently changed the code such a way that I first build the POJO object partially by providing value of simpleProp and type1 in first method. Then call the second method passing partially built object as parameter, second method get value of type2 and other values from the partially build object which it got in parameter. Then it is able to construct object of POJO.

However is there a way/pattern I can use to construct the object nicer/cleaner way?

Thanks

Aucun commentaire:

Enregistrer un commentaire