-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f7c29
commit 861f9a8
Showing
8 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ain/java/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/FoodItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../java/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/GroceryItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
30 changes: 30 additions & 0 deletions
30
...om/CrackCode/designPattern/structuralDesignPattern/adapterPattern/GroceryItemAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...va/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/GroceryProduct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...va/com/CrackCode/designPattern/structuralDesignPattern/adapterPattern/SwiggyStoreApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...Code/designPattern/structuralDesignPattern/adapterPattern/adapterPatternNote.MD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |