Group6 - Design Pattern - MVC - Spring 2021 - OOP 2019CP
Group6 - Design Pattern - MVC - Spring 2021 - OOP 2019CP
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 :
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 is used:
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.
Let's understand the example of adapter design pattern by the above UML diagram.
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
package adapterpattern;
Step 2:
package adapterpattern;
Step 3:
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));
setAccHolderName(customername);
setAccNumber(accno);
setBankName(bankname);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public String getCreditCard()
{
long accno=getAccNumber();
String accholdername=getAccHolderName();
String bname=getBankName();
Step 4:
package adapterpattern;
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:
package adapterpattern;
Step 2:
package adapterpattern;
Step 3:
package adapterpattern;
package adapterpattern;
Step 6:
Creat BirdAdapter class:
package adapterpattern;
Step 7:
package adapterpattern;
System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();
System.out.println("ToyDuck...");
toyDuck.squeak();
OUTPUT:
THE END