Friday, 1 November 2013

Adapter Design pattern - Real Time Example (Universal Adaptor)

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

The Adapter Design Pattern is a structural pattern designed to resolve incompatibility between two interacting components without altering their existing design. To intuitively understand how this pattern functions in software engineering, consider a classic real-world travel dilemma: using an Indian laptop charger in a United States electrical wall socket.

The Real-World Dilemma: Incompatible Plugs and Sockets

Suppose you travel from India to the USA:

  • Your Laptop Charger Plug: Designed with round, cylindrical pins commonly used in India.
  • The US Wall Socket: Engineered to accept only flat, rectangular prongs.

Because the charger plug and wall socket have completely different physical interfaces, they cannot connect directly. You cannot rebuild the building’s electrical socket, nor do you want to redesign your laptop charger.

The Solution: A Universal Travel Adapter

To resolve the conflict, you purchase a Universal Travel Adapter. This device serves as an intermediary bridge:

  • On one side, it exposes female socket holes that accept your cylindrical charger pins.
  • On the other side, it exposes flat rectangular prongs that plug securely into the US electrical wall socket.

By placing the universal adapter in between, the two incompatible physical interfaces work together effortlessly. This is precisely how the Adapter Design Pattern works in software architecture.

Adapter Pattern Mapping

Pattern Role Real-World Component Function
Client Laptop / User Needs power to run and charge
Target Interface US Wall Socket Specification Expects rectangular pin inputs to deliver power
Adaptee Indian Laptop Charger Plug Existing interface with round cylindrical pins
Adapter Universal Travel Adapter Bridges cylindrical inputs to rectangular pin outputs

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=wmOmD-mJH6o

Click the below Image to Enlarge
Adapter Design pattern - Real Time Example (Universal Adaptor)

No comments:

Post a Comment

Tutorials