1

I was going through design pattern lectures, and I came across the adapter pattern which adapts one interface to another. There, i came across WindowAdapter, and I was curious to know how and what implementation WindowAdapter uses.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86

1 Answers1

2

WindowAdapter is a good example of the class adapter pattern. It provides empty implementations of several interfaces, seen here, as a convenient alternative to doing so repeatedly throughout your code. Complete examples may be found here and here. In outline,

    JFrame f = new JFrame("Test");
    f.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            System.out.println(e);
        }
    });

A similar approach is seen in the several other adapters provided in java.awt.event.

In contrast, the object adapter pattern "contains an instance of the class it wraps." The class JDBCAdapter, examined here, is a Swing example that maps the JDBC interface to the TableModel interface."

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    But the Window adapter is not matching one interface to another interface, it is providing just an implementation. where as adapter pattern matches one interface to another using some adapter classes http://www.javatpoint.com/adapter-pattern – Rahul Singh Oct 04 '16 at 08:24
  • That's the _object adapter pattern_; I've elaborated above. – trashgod Oct 04 '16 at 08:35
  • To reiterate Rahul's question: the Adapter pattern (both class adapter and object adapter variations of it) involves a target, an adaptee and an adapter. In WindowAdapter, the target is a WindowListener; the adapter is a WindowAdapter; but _where's the adaptee_? – k314159 Aug 06 '20 at 21:58
  • @k314159: I'm not sure; please ping me if you add a related answer here or pose a new question on this topic. – trashgod Aug 07 '20 at 00:30