Quickly learning the Adapter Design Pattern in Java

Post to Twitter

Today I’ll show one way to use the Adapter Design Pattern. This design pattern is also sometimes referred to as the “Wrapper Pattern” . As the name suggests and as Wikipedia tells us: “the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) translates one interface for a class into a compatible interface”. Today we will create code to adapt some existing code to use a new back end.

The scenario is this: Your company takes information from your servers and passes it to another company. For the longest time the third party company has asked for the information in a simple string format. Now they want you to supply it in an XML format. Rather than rewrite a bunch of code you decide to use the adapter pattern.

Here is your business object that is loaded with data and up until now was passed to the other company:

public interface IMyCompanyWidget
{
	public String getWidgetModel();
	public void setWidgetModel(String model);
	public String getWidgetColor();
	public void setWidgetColor(String color);
}

Here is the class that implements the above interface.

public class MyCompanyWidget implements IMyCompanyWidget
{
	private String widgetColor;
	private String widgetModel;

	@Override
	public String getWidgetColor()
	{
		return widgetColor;
	}

	@Override
	public String getWidgetModel()
	{
		return widgetModel;
	}

	@Override
	public void setWidgetColor(String model)
	{
		this.widgetColor = model;

	}

	@Override
	public void setWidgetModel(String model)
	{
		this.widgetModel = model;
	}
}

Here is the new interface format you need to now follow to hand back XML to the other company:

public interface IMyNewCompanyWidget
{
	public String getWidgetDataXML();
	public void setWidgetDataXML(String xml);
}

Here is the class that implements the new interface:

public class MyNewCompanyWidget implements IMyNewCompanyWidget
{
	private String xmlData;

	@Override
	public String getWidgetDataXML()
	{
		return xmlData;
	}

	@Override
	public void setWidgetDataXML(String xml)
	{
		this.xmlData = xml;
	}
}

Now we need to build the adapter:

public class OldToNewWidgetAdapter implements IMyNewCompanyWidget
{
	private String xmlData;
	private MyCompanyWidget myCompanyWidget;

	public OldToNewWidgetAdapter(MyCompanyWidget mcw)
	{
		this.myCompanyWidget = mcw;
		this.xmlData = "<xml><widget><widgetColor>" +
			this.myCompanyWidget.getWidgetColor() + "</widgetColor><widgetModel>" +
			this.myCompanyWidget.getWidgetModel() + "</widgetModel></widget></xml>";
	}

	@Override
	public String getWidgetDataXML()
	{
		return xmlData;
	}

	@Override
	public void setWidgetDataXML(String xml)
	{
		this.xmlData = xml;
	}
}

Lets test this out now:

public class RunWidgetAdapter
{
	public static void main(String[] args)
	{
		// Here is the old object
		MyCompanyWidget myCompanyWidget = new MyCompanyWidget();
		myCompanyWidget.setWidgetColor("green");
		myCompanyWidget.setWidgetModel("A45TG3");

		// Now run the adapter
		OldToNewWidgetAdapter adapter = new OldToNewWidgetAdapter(myCompanyWidget);
		System.out.println(adapter.getWidgetDataXML());
	}
}

Our result is exactly what we want:

<xml>
  <widget>
    <widgetColor>green</widgetColor>
    <widgetModel>A45TG3</widgetModel>
  </widget>
 </xml>

Pretty simple but hopefully its a clear way to understand the Adapter Design Pattern.

Post to Twitter

This entry was posted in Design Patterns, Java. Bookmark the permalink.

One Response to Quickly learning the Adapter Design Pattern in Java

  1. thx says:

    thx a ton

Comments are closed.