Composite Design Pattern in Java
Vivek Kurmi
Posted on November 18, 2023
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();
}
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
}
}
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();
}
}
}
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();
}
}
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
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
Posted on November 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 23, 2023