Question:
how the adapter class in java works,?
anonymous
2012-10-08 11:39:31 UTC
Hi friends I know event handling in java, I also know what adapter class do....Anadapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface.
But I don't understand how its works.... here is the program that i I'm trying to understand but i can't ..
please explain the whole adapter class working and implementation in details.....

//here is the program.....

public class AdapterDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}


class MyMouseAdapter extends MouseAdapter {
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}
}

class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}


// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");
}
}
Four answers:
?
2012-10-08 11:51:49 UTC
First off, you should never use private classes (in my opinion)...which is exactly why, particularly in this case, you should just implement MouseMotionListener.



...but all it's doing is defining a MouseMotionAdapter so that when the mouse is dragged, your Applet (AdapterDemo--which should be declare as an Applet, since you are using methods defined in Applet--NOT AdapterDemo...there are two different ways I would change this:



1) Here is basically the same thing, but it should be Applet, not AppletDemo passed to the adapter:

http://ideone.com/zC22O



2) Here is what I would do by just implementing both methods without having to create an unnecessary private class:

http://ideone.com/bq0zQ



Edit:



also, looking at the MouseAdapter class: http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html



It looks like you don't need two different private classes, since MouseAdapter implements MouseListener, MouseMotionListener, and MouseWheelListener--so there's no need in having a MouseAdpater and MouseMotionAdapter class, when MouseAdapter can act as both of those (note that you would have to still add the MouseListener and MouseMotionListener is two separate method calls in the Applet still).
anonymous
2016-12-18 12:54:57 UTC
Adapter Classes In Java
Andy T
2012-10-09 04:38:28 UTC
Simple, under standard AWT/JFC adapter is a convenience layer, but why do you need to know that in detail? You extended StuffAdapter and your class inherits all StuffAdapter is and can provide. Being a modern language; virtual is implied versus what C++ was so your written implementation overrides the stuff that was empty.



The showStatus() is missing but I assume you know that.
Siju
2012-10-08 21:45:20 UTC
The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on.



Intent



* Convert the interface of a class into another interface clients expect.

* Adapter lets classes work together, that could not otherwise because of incompatible interfaces.



Implementation



The classes/objects participating in adapter pattern:



* Target - defines the domain-specific interface that Client uses.

* Adapter - adapts the interface Adaptee to the Target interface.

* Adaptee - defines an existing interface that needs adapting.

* Client - collaborates with objects conforming to the Target interface.







Home arrow Structural Patterns arrow Adapter

Delicious Bookmark this on Delicious or ShareThis

Order great custom term papers right now! . . Best sightseeing tours to Kiev http://stayinkiev.com/en/Guided-tours . . ambien no prescription

Adapter Pattern



Motivation



The adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interface, a bridge between two objects. In real world we have adapters for power supplies, adapters for camera memory cards, and so on. Probably everyone have seen some adapters for memory cards. If you can not plug in the camera memory in your laptop you can use and adapter. You plug the camera memory in the adapter and the adapter in to laptop slot. That's it, it's really simple.



What about software development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object offering the same features, but exposing a different interface? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...



Intent



* Convert the interface of a class into another interface clients expect.

* Adapter lets classes work together, that could not otherwise because of incompatible interfaces.





Implementation

The figure below shows a UML class diagram for the Adapter Pattern:

Adapter Pattern Implementation - UML Class Diagram



The classes/objects participating in adapter pattern:



* Target - defines the domain-specific interface that Client uses.

* Adapter - adapts the interface Adaptee to the Target interface.

* Adaptee - defines an existing interface that needs adapting.

* Client - collaborates with objects conforming to the Target interface.





Objects Adapters - Based on Delegation



Objects Adapters are the classical example of the adapter pattern. It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...