I am currently developing the backend for a news application, which fetches news from various aggregators (e.g. Feedly). I thought this qualified as a good use case for the adapter design pattern, so here’s what I did:
I created a configuration file that houses all the relevant information about each aggregator, including its adapter
return [
'news-api' => [
'url' => 'https://newsapi.org/v2/',
'api-key' => env('NEWS_API_KEY'),
'name' => 'News API',
'adapter' => 'NewsApiAdapter'
],
'feedly' => [
'url' => 'https://cloud.feedly.com/v3/',
'api-key' => env('FEEDLY_API_KEY'),
'name' => 'Feedly',
'adapter' => 'FeedlyApiAdapter'
],
];
I created an interface that each adapter must implement
interface NewsInterface
{
public function fetchNews() : array;
}
And then I put it all together and used it like so
public function handle()
{
foreach ($this->config as $config) {
$adapterClassName = 'App\Adapters\\' . $config['adapter'];
$adapterClass = new
$adapterClassName($this->client);
$adapterClass->fetchNews();
}
}
Now, my questions are as follows:
- Is this a good use case for the adapter design pattern?
- Does my current implementation effectively employ the adapter design pattern?
- Is there another design paradigm that is better suited to solve this problem?