📅 Week-4 (Day-1) - Understanding the Adapter Design Pattern: Real-World Examples and Code 👩‍💻 What is the Adapter Design Pattern?


NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.
đź’ What is the Adapter Design Pattern?
Adapter converts the interface of a class into another interface that the client expects. It allows classes with incompatible interfaces to work together.
(Hindi: “Adapter ek class ke interface ko dusre interface mein convert karta hai jo client expect karta hai. Isse aise classes jo normally saath kaam nahi kar sakti, wo bhi ek saath kaam kar paati hain”)
đź’ Real-Life Examples to Understand
🧳 Example 1: Imagine you're traveling abroad. India ke socket aur abroad ke socket different hote hain, right? Toh kya karte ho? You carry an adapter plug so that your charger (India socket) can fit into their system. That’s an adapter at work! 🔌
📱 Example 2: You have a Type-C phone charger but your cable is Type-B. Instead of changing your entire phone or cable, you use a converter cable that connects both — that's another real-world adapter example.
đź’ Why is it important?
In real-world software projects, you often need to integrate third-party APIs or legacy systems. Sometimes, those systems return data in formats your app doesn't understand — like XML instead of JSON. But you can't just change the third-party system. So, what do you do?
👉 You write an Adapter — a bridge between their format and your expected format.
đź’ Real-World Use Case: XML to JSON Adapter
🔹 Scenario:
You have a third-party service giving you data in XML format. But your application only understands JSON. Changing the app or the third-party is not an option.
➡️ So, you create an XmlToJsonAdapter to translate XML into JSON.
đź’ UML Class Diagram Explanation (Simplified):
Target Interface: What the client expects (JSON provider)
Adaptee: The third-party service (gives XML)
Adapter: Implements the Target Interface, wraps the Adaptee, and converts XML → JSON
Example UML Diagram :
Standard UML Diagram
Code
// 1. Target interface expected by the client
interface IReports {
// now takes the raw data string and returns JSON
String getJsonData(String data);
}
// 2. Adaptee: provides XML data from a raw input
class XmlDataProvider {
// Expect data in "name:id" format (e.g. "Alice:42")
String getXmlData(String data) {
int sep = data.indexOf(':');
String name = data.substring(0, sep);
String id = data.substring(sep + 1);
// Build an XML representation
return "<user>"
+ "<name>" + name + "</name>"
+ "<id>" + id + "</id>"
+ "</user>";
}
}
// 3. Adapter: implements IReports by converting XML → JSON
class XmlDataProviderAdapter implements IReports {
private XmlDataProvider xmlProvider;
public XmlDataProviderAdapter(XmlDataProvider provider) {
this.xmlProvider = provider;
}
public String getJsonData(String data) {
// 1. Get XML from the adaptee
String xml = xmlProvider.getXmlData(data);
// 2. NaĂŻvely parse out <name> and <id> values
int startName = xml.indexOf("<name>") + 6;
int endName = xml.indexOf("</name>");
String name = xml.substring(startName, endName);
int startId = xml.indexOf("<id>") + 4;
int endId = xml.indexOf("</id>");
String id = xml.substring(startId, endId);
// 3. Build and return JSON
return "{\"name\":\"" + name + "\", \"id\":" + id + "}";
}
}
// 4. Client code works only with IReports
class Client {
public void getReport(IReports report, String rawData) {
System.out.println("Processed JSON: "
+ report.getJsonData(rawData));
}
}
public class AdapterPattern {
public static void main(String[] args) {
// 1. Create the adaptee
XmlDataProvider xmlProv = new XmlDataProvider();
// 2. Make our adapter
IReports adapter = new XmlDataProviderAdapter(xmlProv);
// 3. Give it some raw data
String rawData = "Alice:42";
// 4. Client prints the JSON
Client client = new Client();
client.getReport(adapter, rawData);
// → Processed JSON: {"name":"Alice", "id":42}
}
}
📚 Summary
Adapter Pattern kya karta hai?
Ek system ko dusre system ke sath kaam karne layak banata hai jab unke interface alag hote hain.
Kab use karna chahiye?
Jab aapko kisi dusre API ya service se data lena ho aur format match na kare. Jaise XML data ko JSON me dikhana.
Kaise kaam karta hai?
Ek adapter class banate ho jo XML le kar use JSON me convert karti hai, bina client code ko change kiye.
Week - 4 ( Day-1 ) Completed âś… System Design
NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- https://www.youtube.com/@CoderArmy9 . 🙌
👉 Share this blog with your connections! Let’s keep learning, growing, and supporting one another on this journey. 🚀
✍️ Payal Kumari 👩‍💻
Jai Hind 🇮🇳 | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #Code #LLD #OOP 👩‍💻
Subscribe to my newsletter
Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Kumari
Payal Kumari
I'm a passionate full-stack developer with a strong foundation in the MERN stack—building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, I’ve contributed to and mentored projects in top open source programs like GSSoC’24, SSOC’24, and C4GT’24. As a Google Gen AI Exchange Hackathon ’24 Finalist and Google’s Women Techmakers (WTM) Ambassador, I’ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC ’24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on “Topmate Discover,” I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where I’ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamwork—signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, I’ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether it’s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, I’m driven by the power of community and continuous learning. Let’s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.