jeudi 16 février 2023

Builder Pattern to build object with static classes

I am having a question of building the object in a scenario like ,

   @Builder
   public class Al {
    
    public Logic logic;

    @Value
    @Builder
    public static class Logic{
        public Eq eq;
        public Neq neq;
    }
    
    @Value
    @Builder
    public static class Eq{
        public Arg1 arg1;
        public Arg2 arg2;
    }
    
    @Value
    @Builder
    public static class Neq{
        public Arg1 arg1;
        public Arg2 arg2;
    }
    
    @Value
    @Builder
    public static class Arg1{
        public String myvar;
    }
    
    @Value
    @Builder
    public static class Arg2{
        public String val;
    }
 }

I need to build an object and transform to json similar to

{
  "logic": {
    "neq": {
      "arg1": {
        "var": "pos"
      },
      "arg2": {
        "val": "0"
      }
    }
  }
}

Here , neq under logic is decided by one of the condition. Based on the condition , "neq" may be replaced with "eq" and the logic block look as follows

{
  "logic": {
    "eq": {
      "arg1": {
        "var": "pos"
      },
      "arg2": {
        "val": "0"
      }
    }
  }
}

While forming the object using builder pattern i use something like
Code block

I am interested to know to reduce the duplicate block and making it as single build object.

Aucun commentaire:

Enregistrer un commentaire