This is in continuation to the previous post. Please note that this post will be long one, so if you have time keep reading 🙂
Let me start off with the first pattern i picked up to talk about in Structural patterns category.
It states that “Translating one interface of a component or a class to a compatible interface”, it may not look simple as you read it. What it means is that many times in real world what happens is one component wont fit into another component appropriately. We often tend to overlook the compatibility issues and thus encounter problem while fitting them together to make it work.
If they are newly designed components, then it’s not a major problem for us to worry, because both of them will be designed as perfectly as specified to get coupled, but if one of the component is existing or generic and other is already designed or modification needed, then its a problem for us.
As said above, there are many places which we see in our real life examples where in many components wont fit to the existing components at all.
Assume there is a legacy driver component which your project would be interested in to use. But its interface wont match with yours, and neither you can change in your project to make it work due to design issues or cost of change is huge/complex.
Lets assume the workaround way where in we either did not know about this pattern nor did not want to use it. What would happen is, to fit component A and B together at position Y, due to incompatibility issues you either would change component A or B and make them fit together, but did not realize that either of these component is already broken due to this change or you have violated the design which it was built on.
Next to fit B and C component at X position again due to same reasons you would change B (assumed) and fit them all together, but if these chaining continues a lot more like this, think about the complexity you have added to the software module. After a while, the whole changes made looks so ugly for yourself or for the other developers looking at this components, also its very difficult to understand because what your design says is quite different than what your implementation says, And also to fix a bug or add feature to a component gets quite tedious job. Overall the complexity in maintaining this code part gets really complex in a long run.
As stated above, to make 2 components to co-work together perfectly we are in need of an interface which is compatible to both of the components.
So lets look at the code i wrote which simulates this pattern usage, of course it’s very much simplistic in nature.
As you can see from above code, i here by have an Old target component which i am supposed to work with. As said i also have a new target component which is being developed but its interface wont fit with the Old target, lets look at it:
So now its pretty clear to you that the old and new targets wont directly fit at all. So what we need to do to solve this in-compatibility is to bring in Mr. Adapter. So lets invite him and see what good he can do:
So what Mr. Adapter guy is doing here is, he is acting as a compatible interface between those 2 components. Now all of these component can work happily together in system. Lets see the usage code part of this adapter guy:
The advantage here is, if by some reason one of the component interface gets changed or added, then other one need not to worry much, just the interfacing guy has to be aware of it. So the over all cost of change in the system is very much minimized by using this pattern.
Lets look at couple of real world examples where we do come across such pattern usage:
- A micro SD card surely wont fit into the laptops SD card slot. Instead of changing either of those components we make use of some adapter component as show in the above picture.
- There are many electrical components whose pins are in different shapes, but there are common slots in the walls where you can’t plug any shaped pins. So we make use of adapter plugs there too as shown in pic.
- To fit 2 different sized water pipes together, we make use of Adapter component.
Well, that’s all dear friend i can talk about this pattern. I am sorry if it’s too lengthy, but i wanted to make every point clear.
Look out for my next post in continuation to this.
P.S: Your valuable comments, votes are well appreciated.