Adapter class in java with example

Adapter classes in Java with example

In this guide, you will learn about various adapter classes in Java, their implementation and usefulness in developing GUI projects with an easy and understandable example.

Java adapter classes with example

Before we start with the implementation of adapter classes, Let’s understand what adapter classes in Java really are. The adapter class in Java offers default enforcement of all methods of an interface. But, there is no real need for us to modify all methods of an interface. Then, why do we need to use adapter classes in Java? simply put, adapter classes reduce the coding burden by making the code highly reusable. It enables a way to include relevant patterns in class. Additionally, it can assist different unrelated classes to work simultaneously or together. As we know, the adapter class modifies all the methods of an interface by default so, we are only required to modify some methods.

Now that we understand adapter classes in Java, let’s take an example and start coding to understand its implementation.

First, Let’s import the packages in which we can find the adapter class in Java. Where can you find adapter classes in Java? You can find the adapter classes in the java.awt.event, java.awt.dnd and javax.swing.event packages.

import java.awt.*; import java.awt.event.*;

then, we define a class named ‘example’ which inherits the properties or attributes of the adapter class – MouseMotionAdapter. The class MouseMotionAdapter is an abstract adapter class that can be used for receiving mouse motion events. You should also note that all methods of this class are empty by default.

Читайте также:  Ввод числа

Here, the MouseMotionListener is a java interface that is altered whenever the mouse is moved or dragged about. We are also using setSize(), setLayout(), setVisible() methods. setSize() is a method in the Java vector class that is used to set the size of a vector. The layout is set as null in this program, it means that fixed locations and sizes don’t change with the environment. Do note that, setLayout(null) is very useful for making quick prototypes.

The mouseDragged method is summoned when a cursor is being dragged in a window listening for mouse motion event of type MouseEvent.

In this code, we have set the color of the graphics as blue. And lastly, the getGraphics() method gives us a graphic context for sketching or drawing in the component you designed.

Since we learned about the methods required to implement adapter classes and the functions they perform, let’s take a look at the final code!

import java.awt.*; import java.awt.event.*; public class example extends MouseMotionAdapter < Frame f; example()< f=new Frame("Mouse Motion Adapter"); f.addMouseMotionListener(this); f.setSize(300,300); f.setLayout(null); f.setVisible(true); >public void mouseDragged(MouseEvent e) < Graphics g=f.getGraphics(); g.setColor(Color.BLUE); g.fillOval(e.getX(),e.getY(),20,20); >public static void main(String[] args) < new example(); >>

let’s check out the application of the code we developed!

Источник

Adapter Design Pattern in Java

Adapter Design Pattern in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.

Adapter Design Pattern

adapter design pattern, adapter design pattern in java, adapter pattern, adapter pattern java example

One of the great real life example of Adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket. We will try to implement multi-adapter using adapter design pattern in this tutorial. So first of all we will have two classes — Volt (to measure volts) and Socket (producing constant volts of 120V).

package com.journaldev.design.adapter; public class Volt < private int volts; public Volt(int v)< this.volts=v; >public int getVolts() < return volts; >public void setVolts(int volts) < this.volts = volts; >> 
package com.journaldev.design.adapter; public class Socket < public Volt getVolt()< return new Volt(120); >> 

Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. So first of all we will create an adapter interface with these methods.

package com.journaldev.design.adapter; public interface SocketAdapter

Two Way Adapter Pattern

  1. Class Adapter — This form uses java inheritance and extends the source interface, in our case Socket class.
  2. Object Adapter — This form uses Java Composition and adapter contains the source object.

Adapter Design Pattern — Class Adapter

Here is the class adapter approach implementation of our adapter.

package com.journaldev.design.adapter; //Using inheritance for adapter pattern public class SocketClassAdapterImpl extends Socket implements SocketAdapter < @Override public Volt get120Volt() < return getVolt(); >@Override public Volt get12Volt() < Volt v= getVolt(); return convertVolt(v,10); >@Override public Volt get3Volt() < Volt v= getVolt(); return convertVolt(v,40); >private Volt convertVolt(Volt v, int i) < return new Volt(v.getVolts()/i); >> 

Adapter Design Pattern — Object Adapter Implementation

Here is the Object adapter implementation of our adapter.

package com.journaldev.design.adapter; public class SocketObjectAdapterImpl implements SocketAdapter < //Using Composition for adapter pattern private Socket sock = new Socket(); @Override public Volt get120Volt() < return sock.getVolt(); >@Override public Volt get12Volt() < Volt v= sock.getVolt(); return convertVolt(v,10); >@Override public Volt get3Volt() < Volt v= sock.getVolt(); return convertVolt(v,40); >private Volt convertVolt(Volt v, int i) < return new Volt(v.getVolts()/i); >> 

Notice that both the adapter implementations are almost same and they implement the SocketAdapter interface. The adapter interface can also be an abstract class. Here is a test program to consume our adapter design pattern implementation.

package com.journaldev.design.test; import com.journaldev.design.adapter.SocketAdapter; import com.journaldev.design.adapter.SocketClassAdapterImpl; import com.journaldev.design.adapter.SocketObjectAdapterImpl; import com.journaldev.design.adapter.Volt; public class AdapterPatternTest < public static void main(String[] args) < testClassAdapter(); testObjectAdapter(); >private static void testObjectAdapter() < SocketAdapter sockAdapter = new SocketObjectAdapterImpl(); Volt v3 = getVolt(sockAdapter,3); Volt v12 = getVolt(sockAdapter,12); Volt v120 = getVolt(sockAdapter,120); System.out.println("v3 volts using Object Adapter="+v3.getVolts()); System.out.println("v12 volts using Object Adapter="+v12.getVolts()); System.out.println("v120 volts using Object Adapter="+v120.getVolts()); >private static void testClassAdapter() < SocketAdapter sockAdapter = new SocketClassAdapterImpl(); Volt v3 = getVolt(sockAdapter,3); Volt v12 = getVolt(sockAdapter,12); Volt v120 = getVolt(sockAdapter,120); System.out.println("v3 volts using Class Adapter="+v3.getVolts()); System.out.println("v12 volts using Class Adapter="+v12.getVolts()); System.out.println("v120 volts using Class Adapter="+v120.getVolts()); >private static Volt getVolt(SocketAdapter sockAdapter, int i) < switch (i)< case 3: return sockAdapter.get3Volt(); case 12: return sockAdapter.get12Volt(); case 120: return sockAdapter.get120Volt(); default: return sockAdapter.get120Volt(); >> > 

When we run above test program, we get following output.

v3 volts using Class Adapter=3 v12 volts using Class Adapter=12 v120 volts using Class Adapter=120 v3 volts using Object Adapter=3 v12 volts using Object Adapter=12 v120 volts using Object Adapter=120 

Adapter Design Pattern Class Diagram

adapter pattern class diagram, adapter design pattern, adapter pattern, adapter design pattern in java

Adapter Design Pattern Example in JDK

Some of the adapter design pattern example I could easily find in JDK classes are;

  • java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)

That’s all for adapter design pattern in java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Оцените статью