Adapter
Last updated
Last updated
Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
The Adapter design pattern is a structural pattern that allows objects with incompatible interfaces to work together.
It acts as a bridge between two incompatible interfaces by converting the interface of a class into another interface that a client expects.
This pattern is particularly useful when integrating new features into existing systems or when interfacing with third-party libraries.
In this example:
OldDataFetcher
represents the old interface with the fetchData
method.
NewDataFetcher
represents the new interface with the retrieveData
method.
DataAdapter
acts as an adapter, inheriting from NewDataFetcher
but internally using OldDataFetcher
to fetch data. It overrides the retrieveData
method to adapt the old interface to the new one.
When we create an instance of DataAdapter
, we pass an instance of OldDataFetcher
to its constructor.
When calling retrieveData()
on the adapter, it internally calls fetchData()
of the OldDataFetcher
, making the old interface compatible with the new one.