diff --git a/Programs/Adapter/AdapterTest.java b/Programs/Adapter/AdapterTest.java new file mode 100644 index 0000000..dc66b9b --- /dev/null +++ b/Programs/Adapter/AdapterTest.java @@ -0,0 +1,79 @@ +package Adapter; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +public class AdapterTest { + + // Custom implementation of ServiceInterface for testing purposes + static class TestService implements ServiceInterface { + private String capturedData; // Stores the last action performed + + @Override + public void performAction(String data) { + this.capturedData = data; + } + + public String getCapturedData() { + return capturedData; + } + } + + @Test + void testClientRequestThroughAdapter() { + // Arrange: Create a test implementation of ServiceInterface + TestService testService = new TestService(); + + // Wrap the test service with the Adapter + TargetInterface adapter = new Adapter(testService); + + // Create the Client with the Adapter + Client client = new Client(adapter); + + // Act: Client sends a request + String testRequest = "Test Request"; + client.execute(testRequest); + + // Assert: Verify the service captured the correct input + assertEquals(testRequest, testService.getCapturedData(), "The service should receive the correct request from the adapter."); + } + + @Test + void testAdapterDirectTranslation() { + // Arrange: Create a test implementation of ServiceInterface + TestService testService = new TestService(); + + // Wrap the test service with the Adapter + TargetInterface adapter = new Adapter(testService); + + // Act: Call the adapter's method directly + String testInput = "Adapter Test Input"; + adapter.processRequest(testInput); + + // Assert: Verify the service captured the correct input + assertEquals(testInput, testService.getCapturedData(), "The adapter should directly translate the request to the service."); + } + + @Test + void testMultipleRequests() { + // Arrange: Create a test implementation of ServiceInterface + TestService testService = new TestService(); + + // Wrap the test service with the Adapter + TargetInterface adapter = new Adapter(testService); + + // Create the Client with the Adapter + Client client = new Client(adapter); + + // Act: Client sends multiple requests + String request1 = "Request One"; + String request2 = "Request Two"; + + client.execute(request1); + assertEquals(request1, testService.getCapturedData(), "The service should capture the first request."); + + client.execute(request2); + assertEquals(request2, testService.getCapturedData(), "The service should capture the second request."); + } +} \ No newline at end of file diff --git a/Programs/Adapter/ServiceInterface.java b/Programs/Adapter/ServiceInterface.java new file mode 100644 index 0000000..97aed7b --- /dev/null +++ b/Programs/Adapter/ServiceInterface.java @@ -0,0 +1,5 @@ +package Adapter; + +public interface ServiceInterface { + void performAction(String data); +} \ No newline at end of file diff --git a/Programs/Adapter/TargetInterface.java b/Programs/Adapter/TargetInterface.java new file mode 100644 index 0000000..6366d02 --- /dev/null +++ b/Programs/Adapter/TargetInterface.java @@ -0,0 +1,5 @@ +package Adapter; + +public interface TargetInterface { + void processRequest(String input); +} \ No newline at end of file diff --git a/Programs/Adapter/adapter.java b/Programs/Adapter/adapter.java new file mode 100644 index 0000000..b797526 --- /dev/null +++ b/Programs/Adapter/adapter.java @@ -0,0 +1,15 @@ +package Adapter; + +public class Adapter implements TargetInterface { + private final ServiceInterface service; + + public Adapter(ServiceInterface service) { + this.service = service; + } + + @Override + public void processRequest(String input) { + // Translating the Client's request to the Service's interface + service.performAction(input); + } +} \ No newline at end of file diff --git a/Programs/Adapter/client.java b/Programs/Adapter/client.java new file mode 100644 index 0000000..3861560 --- /dev/null +++ b/Programs/Adapter/client.java @@ -0,0 +1,14 @@ +package Adapter; + +public class Client { + + private final TargetInterface target; + + public Client(TargetInterface target) { + this.target = target; + } + + public void execute(String request) { + target.processRequest(request); + } +} \ No newline at end of file diff --git a/README.md b/README.md index cb3d676..fc064ac 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ It is very easy to contribute, you may follow these steps - 99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list 100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring. 101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java +103. [adapter pattern](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Adapter/adapter.java)- adapter design pattern in java # Contributors - ## A big thank you to all our contributors!!!