Adapter Pattern

Adapter Pattern

Adapter pattern is design pattern which translates one interface for class into another compatible interface.

It allows classes to work together which is normally not because of incompatible interfaces. Adapter converts interface call to its original interface. For example when transforming the format of dates (e.g. String to MM/DD/YYYY or DD/MM/YYYY).

Structure:
There are two types of adapter patterns:

  • Object Adapter pattern
  • Class Adapter pattern

Object Adapter pattern: In this pattern, adapter keeps an instance of class it wraps. Adapter makes calls to instance of wrapped object.

Class implementation example:

  • Create Adaptee class
import java.text.SimpleDateFormat;
import java.util.Date;

public class Adaptee {

	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM/dd/yyyy");

	public String convertDateFormat(Date date){
		return simpleDateFormat.format(date);

	}

}

 

  • Create Adapter class
import java.util.Date;

public class Adapter {

	Adaptee adaptee;

	public String convertDateInMMDDYYYY(Date date){
		adaptee=new Adaptee();
		return adaptee.convertDateFormat(date);
	}

}

 

  • Create Client class and call Adapter
import java.util.Date;

public class Client {

	public static void main(String[] args) {
		Adapter adapter=new Adapter();
		Date date = new Date();
		String currentDate =adapter.convertDateInMMDDYYYY(date);
		System.out.println(currentDate);

	}

}

 

Class Adapter pattern: Generally adapter uses multiple interfaces to achieve goals. Adapters are created through implementing or inheriting both interface and interface that is pre-existing. As we know java doesn’t supports multiple inheritance so it is common to expect interface will be created as pure interface class.

Class implementation example:

  • Create IAdaptee interface:
import java.util.Date;

public interface IAdaptee {
	String method1(Date date);
}

 

  • Create IAdapteeN interface:
import java.util.Date;

public interface IAdapteeN {
	String methodN(Date date);
}

 

  • Create Adaptee class and implement above two interface:
import java.text.SimpleDateFormat;
import java.util.Date;

public class Adaptee implements IAdaptee,IAdapteeN{

	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM/dd/yyyy");

	public String convertDateFormat(Date date){
		return method1(date);

	}

	@Override
	public String method1(Date date) {
		return methodN(date);
	}

	@Override
	public String methodN(Date date) {
		return simpleDateFormat.format(date);
	}	

}

 

  • Create Adapter class and use class Adaptee:
import java.util.Date;

public class Adapter {

	Adaptee adaptee;

	public String convertDateInMMDDYYYY(Date date){
		adaptee=new Adaptee();
		return adaptee.convertDateFormat(date);
	}

}

 

  • Finally create Client class and use Adapter:
import java.util.Date;

public class Client {

	public static void main(String[] args) {
		Adapter adapter=new Adapter();
		Date date = new Date();
		String currentDate =adapter.convertDateInMMDDYYYY(date);
		System.out.println(currentDate);

	}

}
  • That’s it. Below is list of all design patterns link:

Creational Patterns:

Structural Patterns:

Behavioral Patterns:

Leave a Reply

Your email address will not be published. Required fields are marked *