<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hello Thupten &#187; &#187; adapter design pattern</title>
	<atom:link href="https://www.thupten.feedback/tag/adapter-design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.thupten.feedback</link>
	<description>Just another developer&#039;s blog</description>
	<lastBuildDate>Sat, 09 May 2026 14:41:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Adapter design pattern</title>
		<link>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/</link>
		<comments>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/#respond</comments>
		<pubDate>Sat, 15 Jan 2011 09:26:28 +0000</pubDate>
		<dc:creator><![CDATA[thupten]]></dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[adapter design pattern]]></category>
		<category><![CDATA[design pattern]]></category>

		<guid isPermaLink="false">http://thupten.veryusefulinfo.com/?p=165</guid>
		<description><![CDATA[Adapter pattern adapts a piece of code to work with another. In real world, consider a battery charger for your phone. If you went for vacation to a somewhere in Europe, your charger won&#8217;t fit into the wall outlet. You would need a piece that can wrap your that fits into the socket in Europe. [&#8230;]]]></description>
				<content:encoded><![CDATA[<a href="http://thupten.feedback/wp-content/uploads/2011/01/adapterexample.jpg"><img src="http://thupten.feedback/wp-content/uploads/2011/01/adapterexample-300x122.jpg" alt="" title="adapter pattern" width="300" height="122" class="alignright size-medium wp-image-174" /></a>
Adapter pattern adapts a piece of code to work with another. In real world, consider a battery charger for your phone. If you went for vacation to a somewhere in Europe, your charger won&#8217;t fit into the wall outlet. You would need a piece that can wrap your that fits into the socket in Europe.

Adapter pattern is usually helpful to create new code that is compatible with old legacy code. Using adapter pattern the old code do not need to be changed to work with new code.
<span id="more-165"></span>
In the world of the plugs, there is US plug
<pre class="java"><!--more-->public class USPlug {
    public USPlug(){
        System.out.println("I am a US plug");
    }

    public void plugIn(){
        System.out.println("I fit into a square socket");
    }
}</pre>
And then there is Europe Plug
<pre class="java">public class EuropePlug {
	public EuropePlug(){
		System.out.println("I am a Europe Plug");
	}
	public void plugIn(){
		System.out.println("I fit into circle socket");
	}
}</pre>
We need to put a US plug into a Europe socket. So we need an adapter called UStoEuropeAdapter. This adapter extends the feature of US plug to be compatible with european socket.
So here is the adapter
<pre class="java">public class UStoEuropeAdapter implements USPluggable {

	EuropePlug eplug;

	public UStoEuropeAdapter(EuropePlug ep){
		this.eplug = ep;
		System.out.println("I am the UStoEurope Adapter");
	}

	public void plugIn() {
		this.eplug.plugIn();
	}

}</pre>
The USPluggable interface mandates a class which implements this to make sure to define method to plug into us socket. ie plugIntoSquareSocket()
<pre class="java">public interface USPluggable {
	public void plugIn();
}</pre>
The caller program creates europe plug, wraps it into the adapter and plugs it into the square socket. But since the adapter overwrites the method plugIntoSquareSocket() by calling the plugIntoCircleSocket() instead, now this adapter plugs into circle socket.
<pre class="java">public class MainProgram {
	public static void main(String[] args) {
		EuropePlug ep = new EuropePlug();
		USPluggable myAdapter = new UStoEuropeAdapter(ep);
		myAdapter.plugIn();
	}
}</pre>]]></content:encoded>
			<wfw:commentRss>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
