23

I googled and investigated, but I still need some clarification: Are an adapter class and a controller class similar? If not, in what way do they differ?

Kindly explain.

0

9 Answers 9

28

Adapter is a pattern that provides default (often empty) implementation of interface or abstract class. For example MouseAdapter provides empty implementation of MouseListener interface. It is useful because very often you do not really use all methods declared by interface, so implementing the interface directly is very verbose.

Controller is a part of MVC - Model-View-Controller pattern. No direct relation with Adapter.

3
  • trying to find a list of all Adapter Classes and couldn't find any,, where can i find it?
    – homerun
    Commented Feb 8, 2015 at 14:32
  • There is no such list. As I have written in the answer Adapter is a pattern, so everone can implement it as he wants using any interface he needs. If you want to know a list of adapters implementing specific interface and available in your project use IDE hierarchy (e.g. Ctrl-H in IntelliJ)
    – AlexR
    Commented Feb 8, 2015 at 14:40
  • Such list exist, you don't have to create your own adapter classes. All you have to do is google desired_listener + "adapter" and it'll show up. Example: docs.oracle.com/javase/7/docs/api/java/awt/event/…
    – NoName
    Commented Jul 17, 2019 at 23:31
14

The job of an adapter is primarily to make some object work in a situation where a different interface is expected.

A controller (in an MVC sense) is similar in that it's an intermediary between model and view classes, but different in that it generally embodies a lot more knowledge about how the program is supposed to work. A controller does more than just adapting one interface to another.

Say you've got a Color class that has a setColor(red, green, blue) method. You also have some code that expects a setColor(hue, saturation, value) method. You can create an adapter class that wraps your Color class and implements the expected method in terms of the one your Color class provides.

2
6

Adapter class

  • Adapter class is a simple java class that implements an interface with only EMPTY implementation .
  • Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method

ex--- instead of implementing Servlet(I) if we extends GenericServlet(AC) then we provide implementation for Service()method we are not require to provide implementation for remaining meyhod..

Generic class Acts as ADAPTER class for Servlet(I).

4

An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.

2

an adapter class provide default implementations of all methods in an event listener class,methods are define in that class with empty body; we can override only required methods of that class ,by inherit that class. Adapter class decrease no of lines of coding.

1

every listener interfaces have 1 or more methods. interfaces which contain more than 1 method have adapter classes ...each adapter class contains all the methods of that interface. if we are writing a method of an interface in a class then you should write all the methods ...some of the interfaces of listener contains more than 6 methods ....so inorder to have less burden on programmer they invented these adapter classes.so we extend these adapterclass to our class and we write our method hence it overrides the adaptermethod....so we lose the risk of writing all the methods

0

Adapter objects in android provide view elements for (example) lists - thus they clearly belong to view part of MVC paradigm

0

Advantages of an Adapter Class:

  • If a class extends an Adapter Class, we can override some methods which is needed;
  • It can simplify the creation of the Event handlers in certain situations;
  • It provides an empty implementation of all methods in an EventListener Methods.
1
  • 1
    Can you make an example of a situation in which an Adapter class could simplify the creation of an Event Handler? Thank you. Commented Jun 25, 2014 at 7:43
0

A class which adapts methods of another class by giving different names to essentially the same methods (i.e. push(Stack) instead of add(ArrayList)) is called an adapter class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.