Home / Blog / "Adapter" Design Pattern - a real life example

"Adapter" Design Pattern - a real life example

Mar 11, 2020

I just worked on a legacy project that uses Symfony 4 and Zend Framework 1. Since they exist in the same folder, I had to reuse code from Symfony 4 and implement it in Zend. Fortunatelly both of these use some common classes (Doctrine ORM) so it was possible.

I decided to implement Adapter (Wrapper) Software Design Pattern and that's what I'd like to share with you in a short form.

So basically my Zend app has an "export action" but (unfortunatelly) a previous developer made that action in a separated project, the Symfony app. Now requrement is to add a new service to an existing namespace within Zend app that will reuse this action. Good thing is that developer made a fine service class that has all dependencies passed to __construct(). Thanks to Symfony for this! 

The class I had to reuse is "App/.../ExcelExporter". I have a big service that is composed of a few sub-services and all of these use an interface. So I created a class that implements this interface but I used ExcelExporter from another project. This is how it looks:

And this is usage of the existing service:


As you can see it uses Symfony's TranslatorInterface so I had to make adapter for that as well:

Now I simply create sub-service with:

self::SERVICE__TICKETS_CALCULATION => new TicketsCalculationExportService($this->em, $this->event, $this->view, $this->loggedUser)

Bonus: Use Composer for autoloading

You will see above that I use class from another project. I tried to avoid directly including files, so I added this to Zend composer.json:

Since I think there is lack of real-world examples for design pattern implementations, I hope this will inspire you to maintain legacy software in a better way, so you don't touch existing working code.