java4all@1986 java. Powered by Blogger.

What is meant by Adapater Design Pattern?

>> Friday, May 27, 2011


<pre class='brush:Plain Text;'>
 
ADAPATER DESIGN PATTERN:
         Convert the existing interfaces to a new interface to achieve compatibility and reusability of the unrelated classes in one application. Also known as Wrapper pattern.


Where to use & benefits


  • Try to match an interface(WindowAdapter, etc.)
  • Make unrelated classes work together.
  • Multiple compatibility.
  • Increase transparency of classes.
  • Make a pluggable kit.
  • Delegate objects.
  • Highly class reusable.
  • Achieve the goal by inheritance or by composition
  • Related patterns include



</pre>

Example

The famous adapter classes in Java API are WindowAdapter,ComponentAdapter, ContainerAdapter, FocusAdapter, KeyAdapter, MouseAdapter and MouseMotionAdapter.
As you know, WindowListner interface has seven methods. Whenever your class implements such interface, you have to implements all of the seven methods. WindowAdapter class implements WindowListener interface and make seven empty implementation. When you class subclass WindowAdapter class, you may choose the method you want without restrictions. The following give such an example.
<pre class='brush:Java'>

public interface Windowlistener {
public void windowClosed(WindowEvent e);
public void windowOpened(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowActivated(WindowEvent e);
public void windowDeactivated(WindowEvent e);
public void windowClosing(WindowEvent e);
}
public class WindowAdapter implements WindowListner{
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosing(WindowEvent e){}
}
Here is a test program
import javax.swing.*;
import java.awt.event.*;
class Test extends JFrame {
public Test () {
setSize(200,200);
setVisible(true);
addWindowListener(new Closer());
}
public static void main(String[] args)
{
new Test();
}
class Closer extends WindowAdapter {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
To reuse classes and make new class compatible with existing ones. For example, A clean system is already designed, you want to add more job in, the Extra interface uses adapter pattern to plug in the existing system.
interface Clean {
public void makeClean();
}
class Office implements Clean{
public void makeClean()
{
System.out.println("Clean Office");
}
}
class Workshop implements Clean{ public void makeClean() {
System.out.println("Clean Workshop");
}
}
interface Extra extends Clean{
public void takeCare();
}
class Facility implements Extra{
public void makeClean()
{
System.out.println("Clean Facility");
}
public void takeCare()
{
System.out.println("Care has been taken");
}
}
In order to reuse Workshop and Office classes, we create an adapter interface Extra and add new job takeCare in the system. class Test
{
static void Jobs (Extra job)
{ if(job instanceof Clean)
((Clean)job).makeClean();
if (job instanceof Extra)
((Extra)job).takeCare();
}
public static void main(String[] args)
{
Extra e = new Facility();
Jobs(e);
Clean c1 = new Office();
Clean c2 = new Workshop();
c1.makeClean();
c2.makeClean();
e.makeClean();
}
}
 
 </pre>

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP