Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save florianhoehn/e31e3051b056252c11f71aad3c4da721 to your computer and use it in GitHub Desktop.
Save florianhoehn/e31e3051b056252c11f71aad3c4da721 to your computer and use it in GitHub Desktop.
more sophisticated builder pattern use case
Contact testContact = new ContactBuilder()
.withFirstName('JP')
.withLastName('Coolio')
.withAccount(new AccountBuilder().withRecordType('Coder'))
.build();
@florianhoehn
Copy link
Author

Scenario: we want a contact record with the Name 'JP Coolio'. But we need an account to create a contact record. Usually what do we do? We create two methods in the TestDataFactory class, try to name them in a way that someone else can potentially find the methods again and we call both of the methods wherever we are creating the test data.

With the builder pattern, yes we have to build the infrastructure and when we do have it: voila, we can specify the creation of the account inside the contact as we really don't care about it that much anyway. It's neat. I think. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment