Tuesday, 29 October 2013

Adapter Design pattern - Real Time Exmaple(Translator)

🚀 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 design pattern that allows two incompatible interfaces to communicate seamlessly without modifying their existing behavior. A clear and intuitive real-life analogy to understand this concept is human language translation: communication between two people who speak entirely different languages.

The Communication Problem

Consider two individuals trying to hold a conversation:

  • David (from USA): Speaks and understands only English. He does not know French.
  • Rohit (from France): Speaks and understands only French. He does not know English.

If David wants to communicate with Rohit, or Rohit wants to communicate with David, direct communication is impossible because neither speaks the other's language. Their communication interfaces are incompatible.

The Solution: Introducing John as the Adapter

To resolve this issue, we introduce an intermediary person named John:

  • John knows both English and French fluently.
  • He can listen in English and speak in French, as well as listen in French and speak in English.
  • John acts as the Adapter / Translator bridging David and Rohit.

How the Flow Works

  1. David says "How are you?" in English to John.
  2. John translates "How are you?" into French ("Comment allez-vous?") and passes it to Rohit.
  3. Rohit receives the French message and replies in French ("Très bien, merci") to John.
  4. John translates the French reply back into English ("I am fine, thank you") and speaks it to David.

Through John, David and Rohit converse effectively without either of them having to learn a new language. This is precisely how the Adapter Pattern works in software architecture.

Adapter Pattern Role Mapping

Pattern Role Real-World Participant Role Description
Client David Initiator who understands only English
Target Interface English Interface The language/protocol David expects to interact with
Adaptee Rohit Target receiver who understands only French
Adapter John Translates and converts requests and responses between both parties

Related Tutorials from Ram N Java


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

Click the below Image to Enlarge

Adapter Design pattern - Real Time Exmaple(Translator)
















See also:

  • Adapter Design pattern - Introduction
  • Adapter Design pattern - Real Time Example (Card Reader)
  • Adapter Design pattern - Real Time Example (Mobile Charger)
  • Adapter Design pattern - Real Time Example (Universal Adaptor)
  • Adapter Design pattern - Real Time Example [JDBC Driver]
  • Adapter Design pattern - Class diagram
  • Adapter Design Pattern - Sequence diagram
  • Adapter Design Pattern - Implementation [Language Translator]
  • Adapter Pattern - Implementation [Object Adapter]
  • Adapter Pattern - Implementation [Class Adapter]
  • Adapter Design Pattern - Implementation [Mobile Charger]
  • Adapter Design Pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials