Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Re: problem applying adapter pattern to actionListener


Message #1 by lrau@t... on Fri, 7 Mar 2003 13:46:12
I can not undestand what is your "adapter pattern" problem.
To write an adapter you need :
-an adapter interface to wich you want to adapt 
-an adaptee that you want adapt

Something like this :
-the adapter interface
public interface IAdapter {
  public void paintMe();
}

-the adaptee class :
public class Adaptee {
  public void paintHalfOfMe() {
    //do half paint here
  }
  public void paintTheOtherHalf() {
    //do the rest of paint
  }
}

Now you want to use the Adaptee like you would use any other object 
implementing IAdapter. You need to adapt the Adaptee.
public class AdaptedAdaptee implements IAdapter {
  private Adaptee theEncapsulatedObject;
  public AdaptedAdaptee() {
    theEncapsulatedObject = new Adaptee();
  }
  public void paintMe() {
    theEncapsulatedObject.paintHalfOfMe();
    theEncapsulatedObject.paintTheOtherHalf();
  }
}

This is how you can implement an adapter pattern.
Now... what is your _real_ problem ?

Hope it helped :).

Liviu Rau


> hi everyone:
I> 've got a problem with applying adapter pattern to separates interface 
c> ode from program logic. My problem is don't know how to write an 
A> ctionListener adapter class. Hope you can help me^^....Thank You!!!

> import java.awt.*;
i> mport java.awt.event.*;
i> mport java.applet.Applet;

> public class UIAdapter extends Applet implements ActionListener {
 >  private Button print = new Button("Print");
 >  private Button clear = new Button("Clear");
 >  private Label message = new Label("Message goes here");

>   public void init() {
 >    add(message);
 >    add(print);
 >    add(clear);
 >    print.addActionListener(this);
 >    clear.addActionListener(this);
 >  }
 >  
 >  public void actionPerformed(ActonEvent event) {
 >    Object source = event.getSource();
 >    if (source == print)
 >      message.setText(event.getActionCommand());
 >    else if (source == clear)
 >      message.setText("");
 >  }
}

  Return to Index