Skip to content

Commit

Permalink
add adapter pattern example
Browse files Browse the repository at this point in the history
  • Loading branch information
MdGolam-Kibria committed Feb 6, 2025
1 parent b1f7c29 commit 861f9a8
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;


public class FoodItem implements Item {
private String name;
private BigDecimal foodPrice;
private String shopName;

public FoodItem(String name, BigDecimal foodPrice, String shopName) {
this.name = name;
this.foodPrice = foodPrice;
this.shopName = shopName;
}

@Override
public String getItemName() {
return name;
}

@Override
public BigDecimal getPrice() {
return foodPrice;
}

@Override
public String getRestaurantName() {
return shopName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;

public interface GroceryItem {
String getName();
BigDecimal getPrice();
String getStoreName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;

/**
* This is the example of object level adapter because we are adapting groceryItem using
* instance of that particular class.
*/
public class GroceryItemAdapter implements Item {
private GroceryItem item;

public GroceryItemAdapter(GroceryItem item) {
this.item = item;
}

@Override
public String getItemName() {
return item.getName();
}

@Override
public BigDecimal getPrice() {
return item.getPrice();
}

@Override
public String getRestaurantName() {
return item.getStoreName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;

public class GroceryProduct implements GroceryItem{
private String itemName;
private BigDecimal itemPrice;
private String shopName;

public GroceryProduct(String itemName, BigDecimal itemPrice, String shopName) {
this.itemName = itemName;
this.itemPrice = itemPrice;
this.shopName = shopName;
}

@Override
public String getName() {
return this.itemName;
}

@Override
public BigDecimal getPrice() {
return itemPrice;
}

@Override
public String getStoreName() {
return shopName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;

public interface Item {
String getItemName();
BigDecimal getPrice();
String getRestaurantName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.math.BigDecimal;

public class Main {
public static void main(String[] args) {
SwiggyStoreApp swiggyStoreApp = new SwiggyStoreApp();

//Existing Items
swiggyStoreApp.addItems(new FoodItem("ICE", new BigDecimal(70), "Kaji Store"));
swiggyStoreApp.addItems(new FoodItem("Barger", new BigDecimal(150), "Manik Store"));

//[Adapter] grocery which was incompatible with food. as a new planned service
swiggyStoreApp.addItems(new GroceryItemAdapter(new GroceryProduct("RICE", new BigDecimal(3245), "Rohim Store")));
swiggyStoreApp.addItems(new GroceryItemAdapter(new GroceryProduct("Oil", new BigDecimal(100), "Korim Store")));

//We can create multiple adapter class to adopt multiple functionality as like [Adapter] grocery

swiggyStoreApp.getAllItems().forEach(current -> {
System.out.println(current.getItemName());
System.out.println(current.getPrice());
System.out.println(current.getRestaurantName());
System.out.println();
System.out.println();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.CrackCode.designPattern.structuralDesignPattern.adapterPattern;

import java.util.ArrayList;
import java.util.List;

public class SwiggyStoreApp {
List<Item> items = new ArrayList<>();
public void addItems(Item item) {
items.add(item);
}
public List<Item> getAllItems(){
return items;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


Adapter patterns comes under structural pattern as it deals with how classes are interacting.
As name suggests adapter pattern is used adapt two different incompatible system.

Adapter pattern also helps and be connector between your system and some third party or legacy system.
For example your system expects data in one format to process and third party sends in one format.
Example : Consider you have to invoke some third party soap service which is xml base however your system is built on JSON
with some advanced fields at that time you create JSON-to-XML adapter which will help to interact with thirdparty.


Adapter pattern can be achieved in two ways :
1. Class Level: class level mean to adapt something you are extending and doing inheritance of that class.
2. Object level : Object level mean you keep has-a relationship with class rather doing tight coupling in system.

One of the example is we know java supports both array and list to store data. Now you have legacy system
which is using array and you want to use collection functionalities to do so we have to convert, so Arrays.asList work as adaper
between array to list and then use collections.

A java.io.InputStreamReader translates a byte stream into a character stream, and a java.io.OutputStreamWriter translates a character stream into a byte stream. These classes exemplify the Adapter pattern.
In particular, they change input/output stream interfaces to the required reader/writer interfaces
https://cecs.wright.edu/~tkprasad/courses/ceg860/paper/node26.html


**MY EXAMPLE :**

In this example we will see that Swiggy is selling food app now suddenly lockdown arise
and they thought to provide service of delivering grocery items for sometime so they write adapter which
helps them to convert similar to food item without touching their food delivery business.

https://www.javadevjournal.com/java-design-patterns/adapter-design-pattern/

Reference : https://www.programmergirl.com/java-adapter-pattern/

0 comments on commit 861f9a8

Please sign in to comment.