JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Adapter Pattern in java


An adapter pattern helps two incompatible interfaces to work together. This is the real world definition for an adapter. The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

There are many real world examples, the simplest example is power socket and plug. American plug would not fit to British socket, and viceversa. We use power adapter to fix this issue. The adapter design pattern works exactly similar to power adapter. Here is the UML structure for adapter pattern:

Lets take a simple example. In a factory, there is a automated furnance system. The furnance can be controlled only through temperature in fahrenheit format. But Furnance Regulatory systems gets tempareture in centigrade format. To fix this issue, we use adapter pattern.

FurnanceController class, which controls furnance temperature

package com.java2novice.dp.adapter;

public class FurnanceController {

	/**
	 * this method accepts heat in Fahrenheit format
	 * @param heatLevel
	 */
	public void changeFurnanceTemperature(int heatLevel){
		System.out.println("heat the furnance..");
	}
}

Adapter class, which converts temperature from centigrade format to fahrenheit format.

package com.java2novice.dp.adapter;

public class FurnanceControllerAdapter extends FurnanceController{

	/**
	 * this method access temperature only in centigrade format
	 * @param heatLevel
	 */
	public void controlFurnance(int heatLevel){
		// convert temperature from centigrade to fahrenheit formate
		heatLevel = (heatLevel - 32)*5/9;
		changeFurnanceTemperature(heatLevel);
	}
}

Regulatory system sample code:

package com.java2novice.dp.adapter;

public class FurnanceRegulatorySystem {

	public void regulateFurnanceTemperature(){
		/**
		 * here some lines of code gives temperature in centigrade format 
		 */
		FurnanceControllerAdapter fca = new FurnanceControllerAdapter();
		fca.controlFurnance(300);
	}
}
<< Previous Program | Next Program >>

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
Famous Quotations
Do not confuse motion and progress. A rocking horse keeps moving but does not make any progress.
-- Alfred A. Montapert

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.