Adapter Pattern Simple Example
This pattern involves a single class which is responsible to join functionalities of independent or.
Adapter pattern simple example. Turkey worse many don t show multiple adapters for different adaptees someone cited java s arrays aslist as an example of the adapter pattern adapting an interface of only one class to work with another seems a weak example of the gof adapter pattern. Convert the interface of a class into another interface that clients expect. The adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. One of the great real life example of adapter design pattern is mobile charger.
Mobile battery needs 3 volts to charge but the normal socket produces either 120v us or 240v india. According to the gof s definition the intent of this pattern is to. Socket wrenches provide an example of the adapter. For example the vendoradapter shown above is an object adapter because it creates an instance of object composition vendoradaptee.
Adapter lets classes work together that couldn t otherwise because of incompatible interfaces to understand this definition let s use a real world simple example. Points of interest. This is the desired interface class which will be used by the clients. Let s understand the example of adapter design pattern by the above uml diagram.
Adapter pattern falls under structural pattern of gang of four gof design patterns in net. The adapter could also be thought of as a wrapper. There are the following specifications for the adapter pattern. Many examples of adapter are trivial or unrealistic rectangle vs.
Now we need to use the same app for our client in the uk that wants the same results but in kilometers per hour km h. It is especially used for toolkits and libraries. In these situations it s better to apply the adapter pattern. Example of adapter pattern.
In this article i would like share what is adapter pattern and how is it work. Socket squarepeg vs roundpeg duck vs. For this example the adapter class needs to fulfill two tasks. Adapter pattern works as a bridge between two incompatible interfaces.
A socket attaches to a ratchet provided that the size of the drive is the. Adapter design pattern allows a system to use classes of another system that is incompatible with it. Adapter pattern example consider a scenario in which there is an app that s developed in the us which returns the top speed of luxury cars in miles per hour mph. The adapter design pattern is easy to implement and ensures calling the existing code which was otherwise difficult because their interfaces being incompatible.
What is adapter design pattern.