0% found this document useful (0 votes)
122 views

Group6 - Design Pattern - MVC - Spring 2021 - OOP 2019CP

The document discusses the adapter pattern in Java. It defines the adapter pattern as converting the interface of one class into another interface that clients expect. The objectives are to understand what the adapter pattern is, when it is used, its advantages, and how to implement it. The key aspects are: 1. The adapter pattern allows classes with incompatible interfaces to work together by providing a standard interface for existing methods. 2. An adapter class acts as a wrapper that converts the interface of an existing "adaptee" class to the interface expected by clients. 3. This allows classes to interact without modifying their code and improves flexibility and reusability.

Uploaded by

Abuzar Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Group6 - Design Pattern - MVC - Spring 2021 - OOP 2019CP

The document discusses the adapter pattern in Java. It defines the adapter pattern as converting the interface of one class into another interface that clients expect. The objectives are to understand what the adapter pattern is, when it is used, its advantages, and how to implement it. The key aspects are: 1. The adapter pattern allows classes with incompatible interfaces to work together by providing a standard interface for existing methods. 2. An adapter class acts as a wrapper that converts the interface of an existing "adaptee" class to the interface expected by clients. 3. This allows classes to interact without modifying their code and improves flexibility and reusability.

Uploaded by

Abuzar Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Objectives:

Since this tutorial has been prepared for the beginners to help them understand the
basic concept related to Adapter pattern in java. So the main objectives of this
research work is :

1. To Understand what the Adapter pattern is in java


2. To elaborate the situations where Adapter pattern is implemented
3. Key Advantages of Adapter pattern in java
4. To Understand the implementation of pattern with the help of class diagram
5. To perform the examples and exercise to understand the Pattern

Pattern Name:
The name of pattern which we will discuss in this tutorial in detail is “Adapter
Pattern”.

Introduction :
The adapter pattern convert the interface of a class into another interface clients
expect. Adapter lets classes work together that couldn’t otherwise because of
incompatible interfaces.

This pattern is easy to understand as the real world is full of adapters.    For
example consider a USB to Ethernet adapter. We need this when we have an
Ethernet interface on one end and USB on the other. Since they are incompatible
with each other. we use an adapter that converts one to other. This example is
pretty analogous to Object Oriented Adapters. In design, adapters are used when
we have a class (Client) expecting some type of object and we have an object
(Adaptee) offering the same features but exposing a different interface.

To use an adapter:
1. The client makes a request to the adapter by calling a method on it using the
target interface.
2. The adapter translates that request on the adaptee using the adaptee
interface.
3. Client receive the results of the call and is unaware of adapter’s presence .
Advantages Of Adapter Pattern:

 It allows two or more previously incompatible objects to interact.


 It allows reusability of existing functionality
 Helps achieve reusability and flexibility.
 Client class is not complicated by having to use a different interface and can
use polymorphism to swap between different implementations of adapters

Situations where Adapter pattern is implemented:

It is used:

1. When an object needs to utilize an existing class with an incompatible


interface.
2. When you want to create a reusable class that cooperates with classes which
don't have compatible interfaces.
3. When you want to create a reusable class that cooperates with classes which
don't have compatible interfaces.
4. The adapter design pattern is a structural design pattern that allows two
unrelated/uncommon interfaces to work together. In other words, the
adapter pattern makes two incompatible interfaces compatible without
changing their existing code.
5. Interfaces may be incompatible, but the inner functionality should match
the requirement.
6. The adapter pattern is often used to make existing classes work with
others without modifying their source code.
7. Adapter patterns use a single class (the adapter class) to join
functionalities of independent or incompatible interfaces/classes.
8. The adapter pattern also is known as the wrapper, an alternative
naming shared with the decorator design pattern.
9. This pattern converts the (incompatible) interface of a class (the
adaptee) into another interface (the target) that clients require.
10. The adapter pattern also lets classes work together, which,
otherwise, couldn't have worked, because of the incompatible
interfaces.
11. For example, let's take a look at a person traveling in different
countries with their laptop and mobile devices. We have a different
electric socket, volt, and frequency measured in different countries and
that makes the use of any appliance of one country to be freely used in
a different country. In the United Kingdom, we use Type G socket with
230 volts and 50 Hz frequency. In the United States, we use Type A
and Type B sockets with 120 volts and 60 Hz frequency. In India, we
use Type C, Type D. and Type M sockets with 230 volts and 50 Hz.
lastly, in Japan, we use Type A and Type B sockets with 110 volts and
50 Hz frequency. This makes the appliances we carry incompatible with
the electric specifications we have at different places.
12. This makes the adapter tool essential because it can
make/convert incompatible code into compatible code. Please notice
here that we have not achieved anything additional here there is no
additional functionality, only compatibility.

Class Diagram:
The client sees only the target interface and not the adapter. The adapter
implements the target interface. Adapter delegates all requests to Adaptee.

Adapter Design Pattern Implementation

Let's understand the example of adapter design pattern by the above UML diagram.

UML for Adapter Pattern:

There are the following specifications for the adapter pattern:

Skip Ad

 Target Interface: This is the desired interface class which will be used by the
clients.
 Adapter class: This class is a wrapper class which implements the desired
target interface and modifies the specific request available from the Adaptee
class.
 Adaptee class: This is the class which is used by the Adapter class to reuse
the existing functionality and modify them for desired use.
 Client: This class will interact with the Adapter class.
Implementation of above UML:
Step 1

Create a CreditCard interface (Target interface)

package adapterpattern;

public interface CreditCard {


public void giveBankDetails();
public String getCreditCard();
}
// End of the CreditCard interface

Step 2:

Create a BankDetails class (Adaptee class)

package adapterpattern;

public class BankDetails {


private String bankName;
private String accHolderName;
private long accNumber;

public String getBankName() {


return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getAccHolderName() {
return accHolderName;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
public long getAccNumber() {
return accNumber;
}
public void setAccNumber(long accNumber) {
this.accNumber = accNumber;
}
}

//End of BankDetail Class

Step 3:

Create a BankCustomer class (Adapter class).

package adapterpattern;

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BankCustomer extends BankDetails implements CreditCard {
public void giveBankDetails(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the account holder name :");


String customername=br.readLine();
System.out.print("\n");

System.out.print("Enter the account number:");


long accno=Long.parseLong(br.readLine());
System.out.print("\n");

System.out.print("Enter the bank name :");


String bankname=br.readLine();

setAccHolderName(customername);
setAccNumber(accno);
setBankName(bankname);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public String getCreditCard()
{
long accno=getAccNumber();
String accholdername=getAccHolderName();
String bname=getBankName();

return ("The Account number "+accno+" of "+accholdername+" in "+bname+ "bank is valid


and authenticated for issuing the credit card. ");
}
}

Step 4:

Create a AdapterPatternDemo class (client class).

package adapterpattern;

public class AdapterPatternDemo {


public static void main(String args[]){
CreditCard targetInterface=new BankCustomer();
targetInterface.giveBankDetails();
System.out.print(targetInterface.getCreditCard());
}
}

Output:

Exercise Question :

Suppose you have a Bird class with fly() , and makeSound()methods. And also
a ToyDuck class with squeak() method. Let’s assume that you are short on
ToyDuck objects and you would like to use Bird objects in their place. Birds
have some similar functionality but implement a different interface, so we can’t
use them directly. So we will use adapter pattern. Here our client would be
ToyDuck and adaptee would be Bird
Below is Java implementation of it:

Step 1:

Creat Bird Interface:

package adapterpattern;

public interface Bird {


// birds implement Bird interface that allows
// them to fly and make sounds adaptee interface
public void fly();
public void makeSound();

Step 2:

Creat Sparrow class that implements Bird:

package adapterpattern;

public class Sparrow implements Bird {

// a concrete implementation of bird


public void fly()
{
System.out.println("Flying");
}
public void makeSound()
{
System.out.println("Chirp Chirp");
}

Step 3:

Create ToyDuck interface

package adapterpattern;

public interface ToyDuck {


// target interface
// toyducks dont fly they just make
// squeaking sound
public void squeak();
}
Step 5:
Creat PlasticToyDuck Class

package adapterpattern;

public class PlasticToyDuck {


public void squeak()
{
System.out.println("Squeak");
}
}

Step 6:
Creat BirdAdapter class:

package adapterpattern;

public class BirdAdapter {


// You need to implement the interface your
// client expects to use.
Bird bird;
public BirdAdapter(Bird bird)
{
// we need reference to the object we
// are adapting
this.bird = bird;
}

public void squeak()


{
// translate the methods appropriately
bird.makeSound();
}
}

Step 7:

Creat the Main class:

package adapterpattern;

public class Main {


public static void main(String args[])
{
Sparrow sparrow = new Sparrow();
ToyDuck toyDuck = new PlasticToyDuck();

// Wrap a bird in a birdAdapter so that it


// behaves like toy duck
ToyDuck birdAdapter = new BirdAdapter(sparrow);

System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();

System.out.println("ToyDuck...");
toyDuck.squeak();

// toy duck behaving like a bird


System.out.println("BirdAdapter...");
birdAdapter.squeak();
}
}

OUTPUT:

THE END

You might also like