Adapter Design Pattern

The adapter is a structural design pattern that enables the collaboration of objects with incompatible interfaces.

You can create an adapter. This is a special object that converts an object’s interface so that another object can understand it.

An adapter wraps one of the objects to hide the complexity of the conversion that occurs behind the scenes. The wrapped object is not even aware of the adapter. For example, you can wrap an object that operates in meters and kilometers with an adapter that converts all data to imperial units such as feet and miles.

Adapters can not only convert data into various formats, they can also help objects with different interfaces to collaborate. Is that how it works:

The adapter gets an interface, compatible with one of the existing objects.
With this interface, the existing object can safely call the adapter’s methods.
Upon receiving a call, the adapter passes the request to the second object, but in the format and order expected by the second object.
Sometimes it is even possible to create a bi-directional adapter that can convert calls in both directions.

Lets look at the example I created as below.

PHP Sample Code

C# Sample Code

By Yuuma




tel. 06-6454-8833(平日 10:00~17:00)