Τρίτη 25 Ιανουαρίου 2022

Adapter pattern

  • What problem does Adapter pattern solve?



Suppose we have a common PriceCalculator interface for:

- CarPriceCalculator 
- TruckPriceCalculator 

in order to call printVehiclePrice() for both of them:




At some point, we may want to add an extra UKPriceCalculator, that doesn't/shouldn't belong (e.g. a third party calculator) to above hierarchy, and thus cannot implement PriceCalculator, and cannot call printVehiclePrice()




  • How does Adapter pattern solve this?


1. Create an Adapter class implementing PriceCalculator interface
2. Add a UKCarPriceCalculator class property


3. Override PriceCalculator's calculatePrice(), referring UKCarPriceCalculator methods


4. Finally, use the new Adapter class in main method









Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?