Adapter Pattern
- Make a Class Fit an Interface
- Simplify Complex Interfaces
- Translate Data Types & Method Names
Classes
class Fahrenheit:
function getTempFahrenheit():
return this.temp
interface Celsius:
function getTempCelsius()
Object Adapter
class FahrenheitToCelsius implements Celsius:
Fahrenheit fahrenheitTemp
function getTempCelsius():
return (fahrenheitTemp.getTempFahrenheit() - 32) * (5 / 9)
Class Adapter
class FahrenheitToCelsius inherits Fahrenheit implements Celsius:
function getTempCelsius():
return (parent.getTempFahrenheit() - 32) * (5 / 9)
Best Practices
- Typically Encapsulation Preferred
- Limit API to Desired Methods
- Hide Underlying Types