Skip to content

Add Backends For Frontends (BFF) pattern (#300)#3543

Open
AnveshSrivastava wants to merge 3 commits into
iluwatar:masterfrom
AnveshSrivastava:backends-for-frontends-pattern
Open

Add Backends For Frontends (BFF) pattern (#300)#3543
AnveshSrivastava wants to merge 3 commits into
iluwatar:masterfrom
AnveshSrivastava:backends-for-frontends-pattern

Conversation

@AnveshSrivastava

Copy link
Copy Markdown

Summary

Closes #300

Implements the Backends For Frontends (BFF) pattern as a new module,
backends-for-frontends. Two client-specific gateways — MobileBff and
DesktopBff — sit in front of a shared set of downstream microservices
(AuthService, CartService, OrderService, SupplierService) and each
aggregates only the services its own client needs, reshaping the result into
a response tailored to that client:

  • MobileBff calls Auth + Cart + Order → returns a lean MobileDashboardResponse
    suited to a phone screen.
  • DesktopBff calls Auth + Order + Supplier → returns a richer
    DesktopDashboardResponse with back-office detail a mobile client never needs.

This satisfies the issue's acceptance criteria: distinct client-specific backends,
data aggregation from multiple downstream services per BFF, and an optimized,
purpose-built API for each client.

What's included

  • Domain models (Product, CartItem, User, Order, SupplierRecord)
  • Downstream service interfaces + in-memory implementations standing in for
    real microservices
  • ClientBff<T> contract + MobileBff / DesktopBff implementations
  • App.java demonstrating both BFFs against the same user
  • JUnit 5 tests for both BFFs and the demo entry point
  • Class diagram (etc/backends-for-frontends.png + .urm.puml), referenced
    from the README
  • README following the repo's standard pattern-page template

Verification

  • mvn -pl backends-for-frontends -am clean test — all tests passing
  • mvn checkstyle:check@validate -pl backends-for-frontends -am — 0 violations
  • mvn spotless:check -pl backends-for-frontends -B — clean
  • Coverage: 98% instructions, 100% methods, 100% classes (JaCoCo)
  • Full reactor build (mvn clean install, module excluded from unrelated flakes
    below) — green

Notes for reviewers

  • Two pre-existing, unrelated test failures were encountered while running the
    full reactor build and confirmed not caused by this change by reproducing
    them in isolation on a clean checkout:
    • rate-limiting-pattern's TokenBucketRateLimiterTest.shouldRefillTokensAfterTime
      is timing-flaky.
    • commander's test suite can hit OutOfMemoryError: unable to create native thread locally depending on OS thread limits, unrelated to this module.
  • While verifying Checkstyle compliance, I found that WhitespaceAround's
    allowEmptyConstructors/allowEmptyTypes properties are unset (default
    false) in the current Checkstyle config, which conflicts with Spotless/
    Google Java Format's formatting of empty {} bodies. This affects any record
    with a fully empty body — I counted at least 16 other existing modules with
    the same latent construct, which only avoid failing because Checkstyle isn't
    bound to the install lifecycle. I worked around it locally in this module's
    6 records by giving each a minimal documented compact constructor rather than
    a bare empty body, but didn't touch the shared Checkstyle config or any other
    module — happy to open a separate follow-up issue for that if it's useful.

Implements the BFF pattern with two client-specific gateways
(MobileBff, DesktopBff) aggregating shared downstream services
(AuthService, CartService, OrderService, SupplierService) into
client-tailored response shapes.

Closes iluwatar#300
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

PR Summary

Implements the Backends For Frontends (BFF) pattern via a new module backends-for-frontends. Adds two client-specific gateways (MobileBff and DesktopBff) in front of shared downstream services (AuthService, CartService, OrderService, SupplierService). Each BFF returns client-tailored DTOs (MobileDashboardResponse, DesktopDashboardResponse). Includes domain models, in-memory service implementations, demo App, unit tests, diagrams, and documentation.

Changes

File Summary
backends-for-frontends/README.md Introduces the BFF pattern with purpose, architecture, and guidance. Documents two client gateways and references to diagrams and README as pattern entry.
backends-for-frontends/etc/backends-for-frontends.png New class diagram image illustrating BFF components and their relationships.
backends-for-frontends/etc/backends-for-frontends.urm.puml PlantUML diagram text describing the BFF, services, and data flow used for documentation and diagrams.
backends-for-frontends/pom.xml Defines Maven module configuration, dependencies, and main class com.iluwatar.bff.App for the BFF module.
backends-for-frontends/src/main/java/com/iluwatar/bff/App.java Demo entry point wiring in-memory downstream services, instantiating MobileBff and DesktopBff, and logging their dashboards for a sample user.
backends-for-frontends/src/main/java/com/iluwatar/bff/bff/ClientBff.java Defines the generic ClientBff<T> contract with getDashboard(String) method.
backends-for-frontends/src/main/java/com/iluwatar/bff/bff/DesktopBff.java Implements Desktop BFF, aggregating AuthService, OrderService, and SupplierService to produce DesktopDashboardResponse with detailed orders and supplier stock.
backends-for-frontends/src/main/java/com/iluwatar/bff/bff/MobileBff.java Implements Mobile BFF, aggregating AuthService, CartService, and OrderService to produce MobileDashboardResponse with cart details and recent orders.
backends-for-frontends/src/main/java/com/iluwatar/bff/bff/package-info.java Package info documenting client-facing BFF implementations per client type (mobile, desktop).
backends-for-frontends/src/main/java/com/iluwatar/bff/dto/DesktopDashboardResponse.java DTO for desktop back-office payload including greeting, loyaltyTier, order statuses, and supplier stock summaries.
backends-for-frontends/src/main/java/com/iluwatar/bff/dto/MobileDashboardResponse.java DTO for mobile payload including greeting, cart metrics, and recent order summaries.
backends-for-frontends/src/main/java/com/iluwatar/bff/dto/package-info.java Package-level documentation for DTOs used by BFFs.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/CartItem.java CartItem record with product and quantity; includes lineTotal() calculation used in dashboards.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/Order.java Order record with id, productName, and status representing downstream data.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/Product.java Product record with id, name, and priceUsd representing downstream data.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/SupplierRecord.java SupplierRecord with productId, supplierName, and stockLevel for desktop data.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/User.java User record representing authenticated user data including displayName and loyaltyTier.
backends-for-frontends/src/main/java/com/iluwatar/bff/model/package-info.java Domain model package documentation shared by all BFFs.
backends-for-frontends/src/main/java/com/iluwatar/bff/package-info.java Backends For Frontends package documentation for BFF library.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/AuthService.java AuthService interface for user lookup used by all BFFs.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/CartService.java CartService interface for retrieving cart data used by MobileBFF.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/OrderService.java OrderService interface for retrieving order history used by all BFFs.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/SupplierService.java SupplierService interface for fetching supplier stock used by DesktopBFF.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryAuthService.java In-memory implementation of AuthService for tests and demos.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryCartService.java In-memory implementation of CartService.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryOrderService.java In-memory implementation of OrderService.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemorySupplierService.java In-memory implementation of SupplierService.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/package-info.java Package-info for in-memory service implementations.
backends-for-frontends/src/main/java/com/iluwatar/bff/service/package-info.java Package-info for downstream service interfaces.
backends-for-frontends/src/test/java/com/iluwatar/bff/AppTest.java JUnit test ensuring App.main runs without throwing exceptions.

autogenerated by presubmit.ai

@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.32%. Comparing base (74d2dbe) to head (2a97a22).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3543      +/-   ##
============================================
+ Coverage     83.24%   83.32%   +0.08%     
- Complexity     4025     4056      +31     
============================================
  Files          1060     1074      +14     
  Lines         14246    14333      +87     
  Branches        686      689       +3     
============================================
+ Hits          11859    11943      +84     
- Misses         2100     2106       +6     
+ Partials        287      284       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The service is looked up by product name throughout the codebase, not
an actual product ID as the previous naming implied.

@HattoriHenzo HattoriHenzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My global review or this PR:

  • Add a Web Front End BFF
  • In the unit tests add InMemory*.java unit tests

*/
public record Order(String id, String productName, String status) {
/** No additional validation required. */
public Order {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a default constructor here. The JVM will define a default canonical constructor. The same rule applies to all the models.

</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No logging instruction in called in the code. You can remove this dependency if not using it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the compact constructors from all 6 records, good catch. They originally existed to satisfy checkstyle:check@validate (which flags empty {} bodies under WhitespaceAround), but I confirmed CI (mvnw clean verify) doesn't actually run Checkstyle, and the same pattern already exists unaddressed in converter/data-transfer-object, so I dropped the workaround to match the rest of the repo.

Also added InMemory* unit tests for all four service implementations (100% coverage on those classes now, closing the gap Codecov flagged), and confirmed slf4j-api is a genuinely required direct dependency — App.java uses it and it's not provided transitively.

On the Web BFF suggestion — issue #300's acceptance criteria only called for two client-specific backends, which Mobile/Desktop satisfy. Happy to add a Web BFF as a follow-up PR, or in this one if you'd prefer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (3)
  • 2a97a22: Address review feedback: remove unnecessary compact constructors, add InMemory* service tests
  • 3b1d5ca: Rename SupplierService lookup key to productName for clarity

The service is looked up by product name throughout the codebase, not
an actual product ID as the previous naming implied.

Implements the BFF pattern with two client-specific gateways
(MobileBff, DesktopBff) aggregating shared downstream services
(AuthService, CartService, OrderService, SupplierService) into
client-tailored response shapes.

Closes #300

Files Processed (30)
  • backends-for-frontends/README.md (1 hunk)
  • backends-for-frontends/etc/backends-for-frontends.png (0 hunks)
  • backends-for-frontends/etc/backends-for-frontends.urm.puml (1 hunk)
  • backends-for-frontends/pom.xml (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/App.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/ClientBff.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/DesktopBff.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/MobileBff.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/package-info.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/dto/DesktopDashboardResponse.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/dto/MobileDashboardResponse.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/dto/package-info.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/CartItem.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/Order.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/Product.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/SupplierRecord.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/User.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/model/package-info.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/package-info.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/AuthService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/CartService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/OrderService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/SupplierService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryAuthService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryCartService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemoryOrderService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/InMemorySupplierService.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/impl/package-info.java (1 hunk)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/service/package-info.java (1 hunk)
  • backends-for-frontends/src/test/java/com/iluwatar/bff/AppTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (4)
  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/MobileBff.java [68-68]

    enhancement: "Resilience: error handling in BFF"

  • backends-for-frontends/src/main/java/com/iluwatar/bff/bff/DesktopBff.java [66-67]

    maintainability: "Maintainability: extract aggregation logic"

  • backends-for-frontends/src/main/java/com/iluwatar/bff/App.java [78-78]

    enhancement: "Resilience: demo main"

  • backends-for-frontends/src/test/java/com/iluwatar/bff/AppTest.java [37-40]

    test: "Tests: verify end-to-end outputs"

@HattoriHenzo HattoriHenzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A part from the build that fails, it LGTM.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backends for Frontends pattern

2 participants