2021-05-05 08:56:28 +00:00
Some API, like the AWS SDK, heavily rely on the builder pattern to create different data structures. Despite all the benefits, this pattern can become really verbose,
2021-05-09 01:17:04 +00:00
especially when dealing with nested structures. In order to reach a more concise code, "Consumer Builders", also called "Consumer Interface" are often introduced.
2021-05-05 08:56:28 +00:00
2021-05-13 01:16:52 +00:00
The idea is to overload the methods taking others structures in a Builder with a Consumer of Builder instead. This enables to use a
2021-05-05 08:56:28 +00:00
2021-05-13 01:16:52 +00:00
lambda instead of nesting another Builder, resulting in more concise and readable code.
2021-05-05 08:56:28 +00:00
This rule reports an issue when the Consumer Builder methods could be used instead of the classical ones.
== Noncompliant Code Example
----
SendEmailRequest.builder()
.destination(Destination.builder()
.toAddresses("to-email@domain.com")
.bccAddresses("bcc-email@domain.com")
.build())
.build();
----
== Compliant Solution
----
SendEmailRequest.builder()
2021-05-09 01:17:04 +00:00
.destination(d -> d.toAddresses("to-email@domain.com").bccAddresses("bcc-email@domain.com"))
2021-05-05 08:56:28 +00:00
.build();
----
== See
* https://aws.amazon.com/fr/blogs/developer/consumer-builders-in-the-aws-sdk-for-java-v2/[Consumer Builders in the AWS SDK for Java v2]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]