Definition:
Converts the interface of a class into another interface based on client class expectation.
Description:
Adapter Pattern allows the incompatible classes to work together without modifying those incompatible classes. 'Adapter' class playing vital role in connecting two incompatible classes.
What are you achieving with the help of Adapter Pattern?
Adapter Patterns allows two different incompatible classes to work together when both incompatible classes are not modifiable to connect.
What is the impact if you don’t use Adapter Pattern?
Incompatible classes can't work together without modifying those incompatible classes.
Aim:
Convert meal information from data table format to list format, to save imported meal information in repository.
Use Case:
Exporting meal information from excel usually produce the data in DataTable format. The client application shall process the meal information if it's in List format. So, the system needs to convert the records from DataTable format to List format to process the records in data access layer and save it in repository.
Problem Statement:
DataTable and List are incompatible formats. Exporting meal information class and client class are not ready to modify their interface structure. However, client class needs to have meal information in List format to save the meal information in repository.
Solution for Problem Statement:
Creating 'Adapter' class, converts the interface of exporting meal information class to client expected interface helping to convert DataTable to List format to process the meal information in repository.
When do you need Adapter Pattern?
When two different incompatible classes need to work together and both classes are not modifiable to connect, then there is a need for Adapter Pattern.
Example for Adapter Pattern:
ImportMealAdaptee : Existing interface, which has meal information in DataTable format.
MealTarget : Defines the interface that client uses, Interface for meal information in List format.
MealAdapter : Converts the existing interface to client expected interface format.
AdapterPatternClient : Uses List format meal information and save the list format meal information in repository.
public enum MealType { Breakfast = 1, Lunch = 2, Dinner = 3, Snack = 4, } public enum MealCourses { MainCourse = 1, Side = 2, Soup = 3, Appetizer = 4, Salad = 5, Dessert = 6, Drink = 7 } public enum CuisineType { American = 1, Asian = 2, European = 3, Oceanian = 4 } public class Meal { public string MealId { get; set; } public string MealName { get; set; } public MealType MealType { get; set; } public MealCourses MealCourse { get; set; } public CuisineType CuisineType { get; set; } } public class ImportMealAdaptee { public DataTable GetMealsInformationFromExcel() { DataTable mealInfo = new DataTable(); //Logic to read excel file and populate the records in datatable Console.WriteLine("Imported meal information from excel sheet and imported information is available in DataTable format"); return mealInfo; } } public abstract class MealTarget { public abstract ListGetMealList(); } public class MealAdapter : MealTarget { ImportMealAdaptee importMealAdaptee = new ImportMealAdaptee(); public override List GetMealList() { DataTable importedMealInfo = importMealAdaptee.GetMealsInformationFromExcel(); //logic convert importedMealInfo datatable to list(List ) Console.WriteLine("Converted imported meal datatable information into list format"); return new List (); } } class AdapterPatternClient { static void Main(string[] args) { MealTarget mealTarget = new MealAdapter(); List mealList = mealTarget.GetMealList(); Console.WriteLine("Received meal information in list format"); Console.ReadLine(); } }