3.1. Problem and Solution

In an application, you may be a working code base that is architecturally sound and stable. However, new functionality is constantly being added that requires use of these existing objects in a different way than they were originally designed. The roadblock may be as simple as the new functionality expecting a different name of a function. It could also be a bit more complex scenario, where the functionality expects slightly different original object behavior.

The solution is to build another object, using the Adapter Design Pattern. This Adapter object works as an intermediary between the original application and the new functionality. The Adapter Design Pattern defines a new interface for an existing object to match what the new object requires.

For the most part, no existing functionality is lost; it's just used or consumed in a different way. You can equate this to an electrical adapter that receives a three-pronged grounded connection and conforms to a two-prong socket. The adapter transparently forwards the alternating current from the prongs but provides a different interface for the grounding functionality. In most common electrical adapters, the grounding functionality is not lost but is instead provided via a grounding wire that should be connected to the screw on the electrical socket container. In the same way, the Adapter Design Pattern aims to help object-oriented code; it creates conversions for the object interfaces.

While it may be tempting ...

Get Professional PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.