Skip to content

adapter design pattern implementation #192

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Programs/Adapter/AdapterTest.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
5 changes: 5 additions & 0 deletions Programs/Adapter/ServiceInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Adapter;

public interface ServiceInterface {
void performAction(String data);
}
5 changes: 5 additions & 0 deletions Programs/Adapter/TargetInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Adapter;

public interface TargetInterface {
void processRequest(String input);
}
15 changes: 15 additions & 0 deletions Programs/Adapter/adapter.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions Programs/Adapter/client.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!
Expand Down