0

Trying to implement Builder pattern where I have a class Contract extending an abstract class service input

I have created Contract Builder as added below, not getting how can I access userInfo of ServiceInput.

*Can not modify ServiceInput class as it is from a different module.

public class Contract extends ServiceInput{

String name;
..
}
public abstract class ServiceInput{

private UserInfo userInfo;
//getter
//setter
..
}
public class ContractBuilder{
String name;

public ContractBuilderwith(
            Consumer<ContractBuilder> builderFunction) {
        builderFunction.accept(this);
        return this;
    }

 public Contract createContract() {
 return new Contract(name);
}

}
Neeti
  • 377
  • 1
  • 4
  • 16
  • Maybe you could have an abstract ServiceInputBuilder, in charge of setting the ServiceInput specific properties, that ContractBuilder would extend? – sp00m Jun 07 '19 at 12:42
  • 2
    To set the `userInfo` from your builder, you must use the methods and/or constructors provided by `ServiceInput`. – Maurice Perry Jun 07 '19 at 12:46

1 Answers1

0

As suggested by @Mauric, able to solve the issue using the method from ServiceInput. In Contract class constructor I have called ServiceInput method to set the userInfo as below:

super.setUserInfo(userInfo);
Neeti
  • 377
  • 1
  • 4
  • 16