Design Patterns for Dummies. The Adapter Pattern
This is the continuing post in a series of post on Design patterns. Presently we are going through the structural patterns. I will be writing about the adapter pattern today. You can read about the other patterns from the following links
Adapter pattern deals with providing a way to use an already present interface that does not meet with the present requirements. We create an adapter with our desired interface and make this class communicate with the classes that implement a different interface.
As seen in the example, the Person class implements the IPerson interface which has a method MetroTravel. But we have a new interface called INewPerson which has a method called CarTravel. So if we have to use the new interface with the old class, we can create an adapter, PersonAdapter and use it as shown. We have to use an adapter when we have a class that has to connect to a mismatching interface or when we want to create a class which might interact with other class which are not yet built or when we have different methods for different purposes or when have have methods which change as called.
In my next post I will be writing about the Facade pattern which would bring us to the end of structural patterns.
Comments