Now we are going to see the Adapter Design Pattern practical implementation using some use case. In the previous video I would have covered the Adapter Design Pattern theory, principles, real life examples and etc If suppose if you haven’t watched that video, please go and watch that video and then come to this video for better understanding purposes. So here what we are trying to take is we are trying to take a use case using real life example. So we will try to implement that real life problem using Adapter Implementation using Java. So the real life use case is if something if suppose if you reside in India Then you would.. know that homes in India would receive 240 voltage as a default voltage from the electric board. If suppose our mobile our mobile battery needs just 3 voltage. Now if we send 240 voltage to our whatever mobile battery, then that mobile battery would explode. So that mobile battery requirement is only 3 voltage. But we have only 240 voltage in the socket board. What we are trying to do is we are trying to create an adapter. Here mobile charger is acting as an adapter that would convert 240 voltage to 3 voltage If some other electrical equipment needs 12 voltage means we can use the adapter to convert 240 volt to 12 voltage. Here, that mobile charger is acting as an adapter. We are trying to implement that use case using Java. So there’s the voltage. So this is a voltage class in voltage class there would be some voltage variable that would evolve the voltage and then socket class. This would return the actual voltage in India the default voltage is 240 voltage. This is the socket adapter interface would have…is containing..the socket adapter interface containing “get240Voltage”, “get12Voltage”, “get3Voltage”. abstract methods. Socket adapter Implementation class implementing socket adapter and then it’s extending socket. So extending socket means this method can be accessed in this class Here, since it’s implementing socket adapter interface. We need to override these three methods in this class. “get240Voltage”, “get12Voltage”, “get3Voltage”. Here, we have created an object for socket class “sock”. Using this “sock” we can give the actual voltage which is 240. So suppose if the client is asking “get240Voltage” then we can return the actual voltage by using this object. “sock.getVoltage” it would attend the actual voltage 240. If suppose if our client is asking to “get12Voltage”, then first we need to get the actual voltage using this object which is 240 and then we need to convert this 240 to 12. 240 by 12. Using this method we could convert the… We can convert the 240 voltage to 12 voltage. In this method we are trying to send 240 volt is 240 divided by 20 which means 12 voltage would be written using this abstract.. using this overriding method. And then get3Voltage method would do the similar process. Here it would get the actual voltage from socket object and then it will try to convert that actual voltage to 3 voltage, 240 by 80 which means 3 voltage. we are using this SocketAdapterImplementation in a adapter demo class. So, We are trying to create.. create an object for SocketAdapterImplementation. Using that object we are trying to access the “get12Voltage” method, “get3Voltage” method and then “get 240Voltage” method. If we execute this program you could also.. you could get more details. Since in the first case We are trying to get the “get12Voltage” we are trying to access “get12Voltage” method So it’s spending 12 voltage and the second scenario, we are trying to invoke “get3Voltage” method. So it’s spending 3 voltage in third scenario, we were trying to invoke “get240Voltage” method. So it’s printing 240 voltage. so this is one of the simple or sample implementation of Adapter Design Pattern in real life using some real-life example. So if suppose if you have any other doubts, you could post those doubts in the comment section. I will try to explain whenever I get time.

Adapter Design Pattern Java program | Adapter Design Pattern Implementation

Leave a Reply

Your email address will not be published. Required fields are marked *