Skip to main content
added 46 characters in body
Source Link
cropredy
  • 73.3k
  • 8
  • 128
  • 284

In addition to Derek's answer, consider the builder pattern

public class WrapperA {

  ClassA classA
  ClassB classB

  public WrapperA withclassA(ClassA val)  {this.classA = val; return this;}
  public WrapperB withclassA(ClassB val)  {this.classB = val; return this;}
}

public class ClassA {
  String sc;
  String rs;
  String ss;
  public ClassA withSc(String val) {this.sc = val; return this;}
  public ClassA withRs(String val) {this.rs = val; return this;}
  public ClassA withSs(String val) {this.ss = val; return this;}
}

public class ClassB {
  .. similar to ClassA
}

and then you can instantiate in one statement

WrapperA wrapA = new WrapperA()
     .withClassA(new ClassA()
         .withRs('abc')
         .withSc('def'
         .withSS('ghi')
     )
     .withClassB(new ClassB()
          .withXX(..)
          ...
     );

This approach avoids too many arg methods that can be flagged by static code analyzers; is infinitely extensible, is very readable, avoids having to know the order of arguments, makes setting up testmethods easier, and provides other benefits of a facade between the object and how it is invoked (i.e. supporting default values)

In addition to Derek's answer, consider the builder pattern

public class WrapperA {

  ClassA classA
  ClassB classB

  public WrapperA withclassA(ClassA val)  {this.classA = val; return this;}
  public WrapperB withclassA(ClassB val)  {this.classB = val; return this;}
}

public class ClassA {
  String sc;
  String rs;
  String ss;
  public ClassA withSc(String val) {this.sc = val; return this;}
  public ClassA withRs(String val) {this.rs = val; return this;}
  public ClassA withSs(String val) {this.ss = val; return this;}
}

public class ClassB {
  .. similar to ClassA
}

and then you can instantiate in one statement

WrapperA wrapA = new WrapperA()
     .withClassA(new ClassA()
         .withRs('abc')
         .withSc('def'
         .withSS('ghi')
     )
     .withClassB(new ClassB()
          .withXX(..)
          ...
     );

This approach avoids too many arg methods that can be flagged by static code analyzers; is infinitely extensible, is very readable, makes setting up testmethods easier, and provides other benefits of a facade between the object and how it is invoked (i.e. supporting default values)

In addition to Derek's answer, consider the builder pattern

public class WrapperA {

  ClassA classA
  ClassB classB

  public WrapperA withclassA(ClassA val)  {this.classA = val; return this;}
  public WrapperB withclassA(ClassB val)  {this.classB = val; return this;}
}

public class ClassA {
  String sc;
  String rs;
  String ss;
  public ClassA withSc(String val) {this.sc = val; return this;}
  public ClassA withRs(String val) {this.rs = val; return this;}
  public ClassA withSs(String val) {this.ss = val; return this;}
}

public class ClassB {
  .. similar to ClassA
}

and then you can instantiate in one statement

WrapperA wrapA = new WrapperA()
     .withClassA(new ClassA()
         .withRs('abc')
         .withSc('def'
         .withSS('ghi')
     )
     .withClassB(new ClassB()
          .withXX(..)
          ...
     );

This approach avoids too many arg methods that can be flagged by static code analyzers; is infinitely extensible, is very readable, avoids having to know the order of arguments, makes setting up testmethods easier, and provides other benefits of a facade between the object and how it is invoked (i.e. supporting default values)

Source Link
cropredy
  • 73.3k
  • 8
  • 128
  • 284

In addition to Derek's answer, consider the builder pattern

public class WrapperA {

  ClassA classA
  ClassB classB

  public WrapperA withclassA(ClassA val)  {this.classA = val; return this;}
  public WrapperB withclassA(ClassB val)  {this.classB = val; return this;}
}

public class ClassA {
  String sc;
  String rs;
  String ss;
  public ClassA withSc(String val) {this.sc = val; return this;}
  public ClassA withRs(String val) {this.rs = val; return this;}
  public ClassA withSs(String val) {this.ss = val; return this;}
}

public class ClassB {
  .. similar to ClassA
}

and then you can instantiate in one statement

WrapperA wrapA = new WrapperA()
     .withClassA(new ClassA()
         .withRs('abc')
         .withSc('def'
         .withSS('ghi')
     )
     .withClassB(new ClassB()
          .withXX(..)
          ...
     );

This approach avoids too many arg methods that can be flagged by static code analyzers; is infinitely extensible, is very readable, makes setting up testmethods easier, and provides other benefits of a facade between the object and how it is invoked (i.e. supporting default values)