Quantcast
Channel: IdioTechie » Design Pattern
Viewing all articles
Browse latest Browse all 10

Let’s adopt the Adapter

$
0
0

Design Pattern Logo Problem Statement:
Have you ever come to a situation where you have been handed over a charge of a code base and were told to update a new functionality reusing the existing components? I guess the answer will be obviously ‘Yes’. And then when you are on the job you realize. “Oh my God …what a mess I am in? The existing code base is a junk code and how will I fit my new functionality code by reuse. I have to rewrite the whole code again from scratch. It’s a real pain to synchronize the old and the new.”
Hang on there mate. Not to worry. Why not adopt the Adapter design pattern here?

What will the Adapter pattern do?
Adapter will convert the interface of a class into another interface as required by the clients. This means that we can make the classes work together that couldn’t otherwise be possible because of incompatible interfaces. This creates a pluggable design kit which increases the re-usability of the existing code. The adapter can also be thought as a wrapper.

This pattern can be compared exactly to a real life adapter. E.g. The UK appliances cannot be used in the European plug socket. However if we use the European adapter then we can use the UK appliances to any European socket.

Problem Statement

Problem Statement - Adapter Pattern

Structure:

Adapter Pattern Structure

Adapter Pattern Structure

In the above diagram there are following components:

  • TargetInterface – This is the desired interface class which will be used by the clients.
  • Adapter – This class is a wrapper class which implements the desired target interface and modifies the specific request available from the Adaptee class.
  • Adaptee – This is the legacy class which is used by the Adapter to reuse the existing functions and modify them for desired use.
  • Client – This class will interact with the Adapter class.

Please find below Sample Code Example:
Adaptee Class:

public class UKPlug {
	private String rectangularStem1="Live";
	private String rectangularStem2="Neutral" ;
	private String rectangularStem3="Earth";

	public String getRectangularStem1() {
		return rectangularStem1;
	}

	public void setRectangularStem1(String rectangularStem1) {
		this.rectangularStem1 = rectangularStem1;
	}
.
.
public void setRectangularStem3(String rectangularStem3) {
		this.rectangularStem3 = rectangularStem3;
	}

}

Target Interface :

public interface PlugConverter {
 public String getEuropePlug();
}

Adapter Class:

public class EuropeanAdapter extends UKPlug implements PlugConverter {
	@Override
	public String getEuropePlug() {
		String cylindricalStem1 = getRectangularStem1();
		String cylindricalStem2 = getRectangularStem2();
		return "converted European Plug Points: " + cylindricalStem1 + " and "
				+ cylindricalStem2;
	}
}

 Client Class:

public class EuropeanPlugPointClient {
	public static void main(String[] args) {
		PlugConverter targetInterface = new EuropeanAdapter();
		System.out.println("Now the European plugpoint connects with"
				+ targetInterface.getEuropePlug());
	}
}

Interesting facts:

  • Adapter pattern is used as wrappers to link with third party applications, frameworks and libraries.
  • The example and code mentioned above represents the Class Adapter. Class adapter uses inheritance instead of composition. It highlights that the Adapter class will extend from the parent Adaptee class. The disadvantage is same with any other standard Java inheritance i.e. if one Adaptee class is extended by the Adaptor it cannot further extend from any other class.
  • Only if the Target is an interface then we can use ‘class adapter’ as we can implement as many interface as possible.
  • There is another type of Adapter which is Object adapter.It uses composition (‘has-a’ relationship) the Adaptee delegates the calls to Adaptee (unlike the class adapters which extends the Adaptee).  Instead of inheriting the base class create adapter by having the base class as attribute inside the adapter. You can access all the methods by having it as an attribute.  The only change needs to be done in the Adapter class.
  • Adapter design pattern is used in Java API for java.io.InputStreamReader and java.io.OutputStreamWriter.
  • The difference between Adapter pattern and Bridge pattern is that Adapter makes things work after they are designed whereas Bridge makes them work before they are designed.
  • Facade defines altogether a new interface whereas Adapter reuses an old interface. Adapter makes two existing interfaces work together as opposed to defining an entirely new one from scratch.
  • The difference between Adapter and Decorator pattern is that Adapter will change the interface of an existing object whereas Decorator enhances another object without changing its interface.

Download Sample Code:

Download Sample Code

 


Viewing all articles
Browse latest Browse all 10

Trending Articles