DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

Composite Design Pattern in Java

Let's consider a scenario where the Composite Pattern is applied in the context of making API calls. Imagine you have an API that provides information about various entities, and you want to create a unified way to handle both individual API calls and composite API calls that involve multiple entities.

Step-by-Step Explanation:

Step 1: Define a Common Interface

// Step 1: Define a common interface for both individual API calls and composites
public interface ApiCall {
    void execute();
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Concrete Classes for Individual API Calls

// Step 2: Create concrete classes for individual API calls
public class SingleEntityApiCall implements ApiCall {
    private String entity;

    public SingleEntityApiCall(String entity) {
        this.entity = entity;
    }

    public void execute() {
        System.out.println("Executing API call for entity: " + entity);
        // Actual logic to make the API call and handle the response
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Composite Class for API Composites

// Step 3: Create a composite class for API composites
import java.util.ArrayList;
import java.util.List;

public class CompositeApiCall implements ApiCall {
    private List<ApiCall> apiCalls = new ArrayList<>();

    public void addApiCall(ApiCall apiCall) {
        apiCalls.add(apiCall);
    }

    public void execute() {
        System.out.println("Executing Composite API call:");
        for (ApiCall apiCall : apiCalls) {
            apiCall.execute();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Putting It All Together

// Step 4: Putting it all together in the main program
public class ApiCallExample {
    public static void main(String[] args) {
        // Create individual API calls
        ApiCall singleEntityCall1 = new SingleEntityApiCall("Entity1");
        ApiCall singleEntityCall2 = new SingleEntityApiCall("Entity2");

        // Create a composite API call and add individual calls
        CompositeApiCall compositeApiCall = new CompositeApiCall();
        compositeApiCall.addApiCall(singleEntityCall1);
        compositeApiCall.addApiCall(singleEntityCall2);

        // Execute API calls
        singleEntityCall1.execute();
        singleEntityCall2.execute();
        compositeApiCall.execute();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Executing API call for entity: Entity1
Executing API call for entity: Entity2
Executing Composite API call:
Executing API call for entity: Entity1
Executing API call for entity: Entity2
Enter fullscreen mode Exit fullscreen mode

In this example, the ApiCall interface serves as a common ground for both individual API calls (SingleEntityApiCall) and composite API calls (CompositeApiCall). The CompositeApiCall class can contain a collection of various API calls, creating a hierarchical structure. The execute method is implemented recursively, allowing you to perform API calls at different levels of the hierarchy.

"Your feedback and ideas are invaluable – drop a comment, and let's make this even better!"

😍 If you enjoy the content, please 👍 like, 🔄 share, and 👣 follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay