Wednesday 18 April 2007

Adapter pattern

BLOGG 7

Adapter pattern

From my basic understand of adapters, I was able to understand to some extent the adapter pattern too after lectures on adapter patterns. Normal adapter is used as an interface to connect another device that cannot directly fit into the other device. For example; UK made devices uses 3 pin whiles most USA devices uses 2 pin, you definitely cannot connect a three pin device into a 2 pin socket on the wall, in order to do so, you need to get an adapter that is capable of accepting the 3 pin plug to be plugged into it and it has a two pin that can enter the socket on the wall, hence it makes the incompatible device to now work, you can now use the device. There also exist adapters with 3 pins to fix into socket in a wall and has the capability for a two pin plug to fix into itself, hence providing an interface. With this brief introductory explanation of an adapter I will now attend to explain the Adapter pattern by Gang of Four. An adapter pattern creates an interface for different classes and allows them to work together. You can use an adapter pattern in the case you want to use an existing class but it interface doesn’t match the one you need. GOF, they define Adapter Pattern “Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.”

You can have a class and object adapters. An adapter pattern provides a way for clients to use two or more incompatible objects and let them communicate and interact. In general, adapter pattern improves reusability.

I now have considerable understand to what recurring problems to apply adapter pattern in designing object oriented systems.

[GoF] GoF, Elements of Reusable Object Oriented Software e-book page 20

No comments: