Lecture 10 - Adapter Design Pattern
Lecture 10 - Adapter Design Pattern
Outline ●
●
UML of Adapter Pattern
Example of Adapter Pattern
● Benefits of Adapter Pattern
Adapter Design Pattern
● The Adapter design pattern is a structural pattern that allows the interface
of an existing class to be used as another interface.
● It acts as a bridge between two incompatible interfaces, making them
work together.
● This pattern involves a single class, known as the adapter, which is
responsible for joining functionalities of independent or incompatible
interfaces.
Components of Adapter Design Pattern
1. Target Interface
● Role: It’s the common interface that the client code interacts with.
2. Adaptee
● Role: It’s the class or system that the client code cannot directly use due to interface
mismatches.
3. Adapter
● Role: It acts as a bridge, adapting the interface of the adaptee to match the target
interface.
4. Client
● Role: It’s the code that benefits from the integration of the adaptee into the system
through the adapter.
Example of Adapter Design Pattern
Example of Adapter Pattern
Let’s say we have a ShopInventory which maintains a list of products. Later on,
we took over another store inventory which sells groceries. We now want to
add those items to our ShopInventory. The problem we have here is that
although the GroceryItem is just a type of product but is unrelated to the
Product interface.
UML of Adapter Pattern
Java of Adapter Pattern
Let’s first start by defining a Product and a ShopInventory class:
public interface Product {
String getName();
double getPrice();
}
public class ShopInventory {
private List<Product> products;
public ShopInventory() {
this.products = new ArrayList<>();
}
public void addProduct(Product product) {
this.products.add(product);
}
public void removeProduct(Product product) {
this.products.remove(product);
}
}
Java of Adapter Pattern
The third-party store that we have just taken over holds
GroceryItem‘s:
//third-party code
public class GroceryItem {
String itemName;
int costPerUnit;
//constructor, getters and setters
}
Java of Adapter Pattern
Since our ShopInventory only holds items of type Product, let’s create an
adapter for the newly introduced GroceryItem:
public class GroceryItemAdapter implements Product {
private GroceryItem groceryItem;
public GroceryItemAdapter(GroceryItem groceryItem) {
this.groceryItem = groceryItem;
}
public String getName() {
return groceryItem.getItemName();
}
public double getPrice() {
return groceryItem.getCostPerUnit();
}
}
Java of Adapter Pattern
With that, we can now add both our regular products and the grocery items
to our ShopInventory:
//code in our main method
ShopInventory inventory = new ShopInventory();
//adding regular store products - ones that implement
Product interface
inventory.addProduct(new CosmeticProduct("Lavie Handbag",
5000.0));
inventory.addProduct(new FitnessProduct("Yoga SmartFit",
2000.75));
//adding GroceryItem to the store using an adapter
GroceryItem groceryItem = new GroceryItem("Wheat Flour",
100);
inventory.addProduct(new GroceryItemAdapter(groceryItem));
Java of Adapter Pattern
With that, we can now add both our regular products and the grocery items
to our ShopInventory:
//code in our main method
ShopInventory inventory = new ShopInventory();
//adding regular store products - ones that implement
Product interface
inventory.addProduct(new CosmeticProduct("Lavie Handbag",
5000.0));
inventory.addProduct(new FitnessProduct("Yoga SmartFit",
2000.75));
//adding GroceryItem to the store using an adapter
GroceryItem groceryItem = new GroceryItem("Wheat Flour",
100);
inventory.addProduct(new GroceryItemAdapter(groceryItem));
Benefits of Adapter Design Pattern
1. Integration of Existing Code
2. Reuse of Existing Functionality
3. Interoperability
4. Client-Server Communication
5. Third-Party Library Integration
References
1. https://www.geeksforgeeks.org/adapter-pattern/
2. https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
3. https://www.javatpoint.com/adapter-pattern
4. https://sourcemaking.com/design_patterns/adapter
Thank You