Download as pdf
Download as pdf
You are on page 1of 18
>. Institute of Information Technology Kottayam 1. Adapter Dr Vengadeswaran, Asst. Prof IOE 321 SDP 1 Intent >. orang * Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate * It provide the interface according to client requirement while using the services of a class with a different interface. * The Adapter Pattern is also known as Wrapper. — ——_ ~~ pte 7 ‘Adapter oe Incompatible —~ \ (Converts requests to Conga tom of te Se request) - Client needs to get the service from Adaptee, which is incompatible “cannot interact directly Dr Vengadeswaran, Asst. IOE 321 SDP > Institute of Information Tenney Advantage of Adapter Pattern ¢ Allows two or more previously incompatible objects to interact. ¢ Allows reusability of existing functionality. Usage of Adapter Pattern ¢ When an object needs to utilize an existing class with an incompatible interface. ¢ When you want to create a reusable class that cooperates with classes which don't have compatible interfaces. Dr Vengadeswaran, Asst. Prof 1OE 821 SDP 3 Indian insu o apter Information Tecolog Kotayan structural design pattern allows objects with incompatible interfaces to collaborate with one ar AP indian institute o Information Technology Kottayam takes all of our XML data and transforms it to JSON making it compatible with the 3-part) of Dr Vengadeswaran, Asst. Prof IOE 321 SDP 7 General Structure information Technolog Kottayam Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate. The Client is a class that contains the existing business logic of the program. The Client Interface describes a protocol that other classes must follow to be able to collaborate with the client code. The Service is some useful class (usually 3rd-party or legacy). The client can’t use this class directly because it has an incompatible interface. The Adapter is a class that’s able to work with both the client and the service: it implements the client interface, while wrapping the service object. Dr Vengadeswaran, Asst. Prof JOE 821 SDP. lent interfaces lent ntrtace = methodt dat) | ‘Adapter ‘Seve -adaptee:Senice |-—>| | retost eat) sericebethod(specllData) specalbats = convsTaSenceFomatst) rum aapee serve etodpeclDa) ementation ieee) tl me EE is(XmlData xmlData) ommendations(XmlData xmlData) MultiRestoApp RULING Kohno Voom S displayMenus(XmlData xmlData) { displayRecommendations(XmlData xmlData) { FancyUIService { displayMenus(JsonData jsonData) { isplayRecommendations(JsonData jsonDa Dr Vengadeswaran, Asst. Prof 10 > GP indian institut o Information Technolog) rset cris Rested ea SIM RMR UTa TRIN etry Tene Onn fancyUIService = FancyUIService() a} TOCA hee dM an § Teen) saenbictte = = convertXmlToJson(xmlData) fancyUIService.displayMenus(jsonData) 1 TEU TOQUE re mES JsonData jsonData = convertXmlToJson(xmlData) fancyUIService.dispLayRecommendations(jsonData) JsonData (XmlData xmlData) { Old Ul public static void main(String[] args) { WA IMuLtiRestoApp multiRestoApp = new MultiRestoApp() ; multiRestoApp.displayMenus(new XmlData()); FancyUIServiceAdapter adapter = new FancyUIServiceAdapter() ; adapter.displayMenus(new XmlData()) ; New UI ~ i Structure SP sec st Kottayam ean eS ap cd + method (data) Dr Vengadeswaran, Asst. Prof IOE 321 SDP 13 houses the existing business logic of the app ere ClientInterface + method (data) Bee PC SCL >. Institute of Information Technology Kottayam defines the set of behaviors that other classes must follow to collaborate with eel RTT tat + servicetethod(speciatData) (MultiRestoApp & IMultiRestoApp) ats i sperd AN ion CeCe + method(data) >. Institute of Information Technology Kottayam some 3'4-party or legacy code that the client can't use directly because it has an incompatible interface RYT 2 See Sct t) (MultiRestoApp & implements the client interface and wraps the service object i IMultiRestoApp) Pee ee Ole + method (data) ee oe ee + method(data) >... Institute of Information Technolog Kottayam it translates the client calls into something that the wrapped service can understand Ry Tate See aC st LU ke) (FancyUlService) Indian institute of Information Technolog Kottayam ao Or and to it creates a enable objects with (acy to with one another that serves asa ) the adapting behavior is now Core mtge and we can Colne m tala without breaking code a, Dr Vengadeswaran, Asst. Prof IOE 321 SDP Principles >. Institute of Information Technology Kottayam Thank You Dr Vengadeswaran, Asst. Prof 1OE 821 SDP 18

You might also like