DEV Community

Abdullah Al Mahmud
Abdullah Al Mahmud

Posted on

🔎Exploring Design Patterns: Adapter Pattern

General Structure:

Target (IJsonAdapter): The interface that clients expect to use

Adaptee (LegacyXmlSystem): The existing class that needs to be adapted

Adapter (XmlToJsonAdapter): The class that bridges between Target and Adaptee

Client: The code that works with the Target interface

public interface IJsonAdapter
{
    string GetJsonData();
}

// Adaptee: Legacy system that needs to be adapted
public class LegacyXmlSystem
{
    public string GetXmlData()
    {
        return "<data> some XML data </data>";
    }
}

// Adapter: Adapter implementation
public class  XmlToJsonAdapter : IJsonAdapter
{
    private LegacyXmlSystem _xmlSystem;

    public XmlToJsonAdapter(LegacyXmlSystem xmlSystem)
    {
        _xmlSystem = xmlSystem;
    }

    public string GetJsonData()
    {
        string xmlData = _xmlSystem.GetXmlData();
        // converting xml to json
        return "{ data: some XML data converted to JSON }";
    }
}
Enter fullscreen mode Exit fullscreen mode

When to Use:

  • Integrating legacy code with new systems
  • Making incompatible interfaces work together
  • Reusing existing classes with incompatible interfaces
  • Converting data formats between systems
  • Maintaining backward compatibility

Benefits:

  • Promotes reusability of existing code
  • Provides clean separation of concerns
  • Makes incompatible code work together without modifying source
  • Follows Single Responsibility Principle
  • Enhances maintainability

Limitations:

  • Can add complexity to the codebase
  • May require creating multiple adapters for different interfaces
  • Could impact performance due to additional layer
  • Might make debugging more challenging
  • Not suitable when extensive interface modifications are needed

The real-world example in the code shows adapting XML data to JSON format. The pattern is particularly useful in scenarios like:

  • Database interface adaptations
  • Third-party library integration
  • API versioning
  • File format conversions
  • Payment gateway integrations

✨ You can find design pattern codes in this repository

✨ Exploring Design Patterns Other Posts

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay