- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 27.3k
Added Microservices UI Client side composition #2698 #3062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
      
        
          +442
        
        
          −0
        
        
          
        
      
    
  
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      17b8999
              
                Added Microservices client side ui composition to the repo.
              
              
                TarunVishwakarma1 8fe2ea9
              
                Improved some checkstyle changes.
              
              
                TarunVishwakarma1 fcaf4d5
              
                Added Random variable to re-use the random instead of creating it eve…
              
              
                TarunVishwakarma1 3343501
              
                Merge branch 'iluwatar:master' into master
              
              
                TarunVishwakarma1 f7d7b28
              
                changed the Pom.xml and upadted the ReadME.md
              
              
                TarunVishwakarma1 280d90e
              
                Changes in the README.md File as per requirement, name of the file ch…
              
              
                TarunVishwakarma1 471ee47
              
                Merge branch 'master' into master
              
              
                iluwatar File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
|  | ||
| # Client-Side UI Composition Pattern: Assembling Modular UIs in Microservices Architecture | ||
|  | ||
| ### **shortTitle**: Client-Side UI Composition | ||
|  | ||
| ### **Description**: | ||
| Learn how the Client-Side UI Composition pattern allows the assembly of modular UIs on the client side, enabling independent teams to develop, deploy, and scale UI components in a microservices architecture. Discover the benefits, implementation examples, and best practices. | ||
|  | ||
| ### **Category**: User Interface | ||
|  | ||
| ### **Language**: JavaScript, HTML, CSS | ||
|  | ||
| ### **Tag**: | ||
| - Micro Frontends | ||
| - API Gateway | ||
| - Asynchronous Data Fetching | ||
| - UI Integration | ||
| - Microservices Architecture | ||
| - Scalability | ||
|  | ||
| --- | ||
|         
                  TarunVishwakarma1 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ## **Intent of Client-Side UI Composition Design Pattern** | ||
|  | ||
| The Client-Side UI Composition Pattern allows the assembly of UIs on the client side by composing independent UI components (Micro Frontends). Each component is developed, tested, and deployed independently by separate teams, ensuring flexibility and scalability in microservices architecture. | ||
|  | ||
| --- | ||
|  | ||
| ## **Also Known As** | ||
|         
                  TarunVishwakarma1 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| - Micro Frontends | ||
| - Modular UI Assembly | ||
| - Client-Side Integration | ||
|  | ||
| --- | ||
|  | ||
| ## **Detailed Explanation of Client-Side UI Composition Pattern with Real-World Examples** | ||
|  | ||
| ### **Real-world Example** | ||
| > In a SaaS dashboard, a client-side composition pattern enables various independent modules like “Billing,” “Reports,” and “Account Settings” to be developed and deployed by separate teams. These modules are composed into a unified interface for the user, with each module independently fetching data from its respective microservice. | ||
|  | ||
| ### **In Plain Words** | ||
| > The Client-Side UI Composition pattern breaks down the user interface into smaller, independent parts that can be developed, maintained, and scaled separately by different teams. | ||
|  | ||
| ### **Wikipedia says** | ||
| >UI composition refers to the practice of building a user interface from modular components, each responsible for fetching its own data and rendering its own content. This approach enables faster development cycles, easier maintenance, and better scalability in large systems. | ||
| --- | ||
|  | ||
| ## **Programmatic Example of Client-Side UI Composition in JavaScript** | ||
|  | ||
| In this example, an e-commerce platform composes its frontend by integrating three independent modules: **CartService**, **ProductService**, and **OrderService**. Each module is served by a microservice and fetched on the client side through an API Gateway. | ||
|  | ||
| `ApiGateway` Implementation | ||
|  | ||
| ```java | ||
| public class ApiGateway { | ||
|  | ||
| private final Map<String, FrontendComponent> routes = new HashMap<>(); | ||
|  | ||
| public void registerRoute(String path, FrontendComponent component) { | ||
| routes.put(path, component); | ||
| } | ||
|  | ||
| public String handleRequest(String path, Map<String, String> params) { | ||
| if (routes.containsKey(path)) { | ||
| return routes.get(path).fetchData(params); | ||
| } else { | ||
| return "404 Not Found"; | ||
| } | ||
| } | ||
| } | ||
|  | ||
| ``` | ||
|  | ||
| `FrontendComponent` Implementation | ||
| ```java | ||
| public interface FrontendComponent { | ||
| String fetchData(Map<String, String> params); | ||
| } | ||
| ``` | ||
| ## Example components | ||
| ```java | ||
| public class ProductComponent implements FrontendComponent { | ||
| @Override | ||
| public String fetchData(Map<String, String> params) { | ||
| return "Displaying Products: " + params.getOrDefault("category", "all"); | ||
| } | ||
| } | ||
|  | ||
| public class CartComponent implements FrontendComponent { | ||
| @Override | ||
| public String fetchData(Map<String, String> params) { | ||
| return "Displaying Cart for User: " + params.getOrDefault("userId", "unknown"); | ||
| } | ||
| } | ||
| ``` | ||
| This approach dynamically assembles different UI components based on the route provided in the client-side request. Each component fetches its data asynchronously and renders it within the main interface. | ||
|  | ||
| --- | ||
|  | ||
| ## **When to Use the Client-Side UI Composition Pattern** | ||
|  | ||
| - When you have a microservices architecture where multiple teams develop different parts of the frontend. | ||
| - When you need to scale and deploy different UI modules independently. | ||
| - When you want to integrate multiple data sources or services into a cohesive frontend. | ||
|  | ||
| --- | ||
|  | ||
| ## **Client-Side UI Composition Pattern JavaScript Tutorials** | ||
|  | ||
| - [Micro Frontends in Action (O'Reilly)](https://www.oreilly.com/library/view/micro-frontends-in/9781617296873/) | ||
| - [Micro Frontends with React (ThoughtWorks)](https://www.thoughtworks.com/insights/articles/building-micro-frontends-using-react) | ||
| - [API Gateway in Microservices (Spring Cloud)](https://spring.io/guides/gs/gateway/) | ||
|  | ||
| --- | ||
|  | ||
| ## **Benefits and Trade-offs of Client-Side UI Composition Pattern** | ||
|  | ||
| ### **Benefits**: | ||
| - **Modularity**: Each UI component is independent and can be developed by separate teams. | ||
| - **Scalability**: Micro Frontends allow for independent deployment and scaling of each component. | ||
| - **Flexibility**: Teams can choose different technologies to build components, offering flexibility in development. | ||
| - **Asynchronous Data Fetching**: Components can load data individually, improving performance. | ||
|  | ||
| ### **Trade-offs**: | ||
| - **Increased Complexity**: Managing multiple micro frontends can increase overall system complexity. | ||
| - **Client-Side Performance**: Depending on the number of micro frontends, it may introduce a performance overhead due to multiple asynchronous requests. | ||
| - **Integration Overhead**: Client-side integration logic can become complex as the number of components grows. | ||
|  | ||
| --- | ||
|  | ||
| ## **Related Design Patterns** | ||
|  | ||
| - [Microservices API Gateway Pattern](https://java-design-patterns.com/patterns/microservices-api-gateway/) – API Gateway serves as a routing mechanism for client-side UI requests. | ||
| - [Backend for Frontend (BFF)](https://microservices.io/patterns/apigateway.html) – BFF pattern helps build custom backends for different UI experiences. | ||
|  | ||
| --- | ||
|  | ||
| ## **References and Credits** | ||
|  | ||
| - [Micro Frontends Architecture (Microfrontends.org)](https://micro-frontends.org/) | ||
| - [Building Microservices with Micro Frontends](https://martinfowler.com/articles/micro-frontends.html) | ||
| - [Client-Side UI Composition (Microservices.io)](https://microservices.io/patterns/client-side-ui-composition.html) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|  | ||
| This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
|  | ||
| The MIT License | ||
| Copyright © 2014-2022 Ilkka Seppälä | ||
|  | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|  | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|  | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
|  | ||
| --> | ||
|  | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.iluwatar</groupId> | ||
| <artifactId>java-design-patterns</artifactId> | ||
| <version>1.26.0-SNAPSHOT</version> | ||
| </parent> | ||
|  | ||
| <artifactId>microservices-client-side-ui-composition</artifactId> | ||
|  | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> | ||
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>1.18.34</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|         
                  TarunVishwakarma1 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| </dependencies> | ||
|  | ||
| <properties> | ||
| <maven.compiler.source>23</maven.compiler.source> | ||
| <maven.compiler.target>23</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|         
                  TarunVishwakarma1 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| </project> | ||
        
          
          
            50 changes: 50 additions & 0 deletions
          
          50 
        
  ...nt-side-ui-composition/src/main/java/com/iluwatar/clientsideuicomposition/ApiGateway.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.iluwatar.clientsideuicomposition; | ||
|  | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|  | ||
| /** | ||
| * ApiGateway class acts as a dynamic routing mechanism that forwards client | ||
| * requests to the appropriate frontend components based on dynamically | ||
| * registered routes. | ||
| * | ||
| * <p>This allows for flexible, runtime-defined routing without hardcoding specific paths. | ||
| */ | ||
| public class ApiGateway { | ||
|  | ||
| // A map to store routes dynamically, where the key is the path and the value | ||
| // is the associated FrontendComponent | ||
| private final Map<String, FrontendComponent> routes = new HashMap<>(); | ||
|  | ||
| /** | ||
| * Registers a route dynamically at runtime. | ||
| * | ||
| * @param path the path to access the component (e.g., "/products") | ||
| * @param component the frontend component to be accessed at the given path | ||
| */ | ||
| public void registerRoute(String path, FrontendComponent component) { | ||
| routes.put(path, component); | ||
| } | ||
|  | ||
| /** | ||
| * Handles a client request by routing it to the appropriate frontend component. | ||
| * | ||
| * <p>This method dynamically handles parameters passed with the request, which | ||
| * allows the frontend components to respond based on those parameters. | ||
| * | ||
| * @param path the path for which the request is made (e.g., "/products", "/cart") | ||
| * @param params a map of parameters that might influence the data fetching logic | ||
| * (e.g., filters, userId, categories, etc.) | ||
| * @return the data fetched from the appropriate component or "404 Not Found" | ||
| * if the path is not registered | ||
| */ | ||
| public String handleRequest(String path, Map<String, String> params) { | ||
| if (routes.containsKey(path)) { | ||
| // Fetch data dynamically based on the provided parameters | ||
| return routes.get(path).fetchData(params); | ||
| } else { | ||
| // Return a 404 error if the path is not registered | ||
| return "404 Not Found"; | ||
| } | ||
| } | ||
| } | 
        
          
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  ...-side-ui-composition/src/main/java/com/iluwatar/clientsideuicomposition/CartFrontend.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.iluwatar.clientsideuicomposition; | ||
|  | ||
| import java.util.Map; | ||
|  | ||
| /** | ||
| * CartFrontend is a concrete implementation of FrontendComponent | ||
| * that simulates fetching shopping cart data based on the user. | ||
| */ | ||
| public class CartFrontend extends FrontendComponent { | ||
|  | ||
| /** | ||
| * Fetches the current state of the shopping cart based on dynamic parameters | ||
| * like user ID. | ||
| * | ||
| * @param params parameters that influence the cart data, e.g., "userId" | ||
| * @return a string representing the items in the shopping cart for a given | ||
| * user | ||
| */ | ||
| @Override | ||
| protected String getData(Map<String, String> params) { | ||
| String userId = params.getOrDefault("userId", "anonymous"); | ||
| return "Shopping Cart for user '" + userId + "': [Item 1, Item 2]"; | ||
| } | ||
| } | 
        
          
          
            40 changes: 40 additions & 0 deletions
          
          40 
        
  ...-composition/src/main/java/com/iluwatar/clientsideuicomposition/ClientSideIntegrator.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.iluwatar.clientsideuicomposition; | ||
|  | ||
| import java.util.Map; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|  | ||
| /** | ||
| * ClientSideIntegrator class simulates the client-side integration layer that | ||
| * dynamically assembles various frontend components into a cohesive user | ||
| * interface. | ||
| */ | ||
| @Slf4j | ||
| public class ClientSideIntegrator { | ||
|  | ||
| private final ApiGateway apiGateway; | ||
|  | ||
| /** | ||
| * Constructor that accepts an instance of ApiGateway to handle dynamic | ||
| * routing. | ||
| * | ||
| * @param apiGateway the gateway that routes requests to different frontend | ||
| * components | ||
| */ | ||
| public ClientSideIntegrator(ApiGateway apiGateway) { | ||
| this.apiGateway = apiGateway; | ||
| } | ||
|  | ||
| /** | ||
| * Composes the user interface dynamically by fetching data from different | ||
| * frontend components based on provided parameters. | ||
| * | ||
| * @param path the route of the frontend component | ||
| * @param params a map of dynamic parameters to influence the data fetching | ||
| */ | ||
| public void composeUi(String path, Map<String, String> params) { | ||
| // Fetch data dynamically based on the route and parameters | ||
| String data = apiGateway.handleRequest(path, params); | ||
| LOGGER.info("Composed UI Component for path '" + path + "':"); | ||
| LOGGER.info(data); | ||
| } | ||
| } | 
        
          
          
            40 changes: 40 additions & 0 deletions
          
          40 
        
  ...-ui-composition/src/main/java/com/iluwatar/clientsideuicomposition/FrontendComponent.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.iluwatar.clientsideuicomposition; | ||
|  | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
|  | ||
| /** | ||
| * FrontendComponent is an abstract class representing an independent frontend | ||
| * component that fetches data dynamically based on the provided parameters. | ||
| */ | ||
| public abstract class FrontendComponent { | ||
|  | ||
| public static final Random random = new Random(); | ||
|  | ||
| /** | ||
| * Simulates asynchronous data fetching by introducing a random delay and | ||
| * then fetching the data based on dynamic input. | ||
| * | ||
| * @param params a map of parameters that may affect the data fetching logic | ||
| * @return the data fetched by the frontend component | ||
| */ | ||
| public String fetchData(Map<String, String> params) { | ||
| try { | ||
| // Simulate delay in fetching data (e.g., network latency) | ||
| Thread.sleep(random.nextInt(1000)); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| // Fetch and return the data based on the given parameters | ||
| return getData(params); | ||
| } | ||
|  | ||
| /** | ||
| * Abstract method to be implemented by subclasses to return data based on | ||
| * parameters. | ||
| * | ||
| * @param params a map of parameters that may affect the data fetching logic | ||
| * @return the data for this specific component | ||
| */ | ||
| protected abstract String getData(Map<String, String> params); | ||
| } | 
        
          
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  ...de-ui-composition/src/main/java/com/iluwatar/clientsideuicomposition/ProductFrontend.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.iluwatar.clientsideuicomposition; | ||
|  | ||
| import java.util.Map; | ||
|  | ||
| /** | ||
| * ProductFrontend is a concrete implementation of FrontendComponent | ||
| * that simulates fetching dynamic product data. | ||
| */ | ||
| public class ProductFrontend extends FrontendComponent { | ||
|  | ||
| /** | ||
| * Fetches a list of products based on dynamic parameters such as category. | ||
| * | ||
| * @param params parameters that influence the data fetched, e.g., "category" | ||
| * @return a string representing a filtered list of products | ||
| */ | ||
| @Override | ||
| protected String getData(Map<String, String> params) { | ||
| String category = params.getOrDefault("category", "all"); | ||
| return "Product List for category '" | ||
| + category | ||
| + "': [Product 1, Product 2, Product 3]"; | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.