Name-Vikash kumar Section-4D3 Roll-11232947
Practical No : 7
Aim: Write program to understand The Adapter Pattern and Facade Pattern.
Adapter Pattern - An Adapter Pattern says that just "converts the interface of a class into another
interface that a client wants".
In other words, to provide the interface according to client requirement while using the services of a class
with a different interface.
The Adapter Pattern is also known as Wrapper.
Advantage of Adapter Pattern
It allows two or more previously incompatible objects to interact.
It allows reusability of existing functionality.
Usage of Adapter pattern: It is used-
When an object needs to utilize an existing class with an incompatible interface.
When you want to create a reusable class that cooperates with classes which don't have compatible
interfaces.
When you want to create a reusable class that cooperates with classes which don't have compatible
interfaces.
UML for Adapter Pattern:
Name-Vikash kumar Section-4D3 Roll-11232947
Implementation of above UML:
Step 1: Create a CreditCard interface (Target interface).
public interface CreditCard {
public void giveBankDetails();
public String getCreditCard();
}
Step 2: Create a BankDetails class (Adaptee class).
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) {
Name-Vikash kumar Section-4D3 Roll-11232947
this.accNumber = accNumber;
}
}
Step 3: Create a BankCustomer class (Adapter class).
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();
}
}
public String getCreditCard() {
long accno=getAccNumber();
Name-Vikash kumar Section-4D3 Roll-11232947
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).
public class AdapterPatternDemo {
public static void main(String args[]){
CreditCard targetInterface=new BankCustomer();
targetInterface.giveBankDetails();
System.out.print(targetInterface.getCreditCard());
}
}
OUTPUT-
Facade Pattern - A Facade Pattern says that just "just provide a unified and simplified interface to a
set of interfaces in a subsystem, therefore it hides the complexities of the subsystem from the client".
In other words, Facade Pattern describes a higher-level interface that makes the sub-system easier to use.
Practically, every Abstract Factory is a type of Facade.
Advantage of Facade Pattern
It shields the clients from the complexities of the sub-system components.
It promotes loose coupling between subsystems and its clients.
Usage of Facade Pattern: It is used -
o When you want to provide simple interface to a complex sub-system.
Name-Vikash kumar Section-4D3 Roll-11232947
o When several dependencies exist between clients and the implementation classes of an abstraction.
UML for Facade Pattern:
Implementation of above UML:
Step 1: Create a MobileShop interface.
public interface MobileShop {
public void modelNo();
public void price();
}
Step 2: Create a Iphone implementation class that will implement Mobileshop interface.
public class Iphone implements MobileShop {
public void modelNo() {
System.out.println(" Iphone 6 ");
}
public void price() {
System.out.println(" Rs 65000.00 ");
}
}
Step 3: Create a Samsung implementation class that will implement Mobileshop interface.
public class Samsung implements MobileShop {
public void modelNo() {
System.out.println(" Samsung galaxy tab 3 ");
}
public void price() {
System.out.println(" Rs 45000.00 ");
}
}
Step 4: Create a Blackberry implementation class that will implement Mobileshop interface .
public class Blackberry implements MobileShop {
public void modelNo() {
Name-Vikash kumar Section-4D3 Roll-11232947
System.out.println(" Blackberry Z10 ");
}
public void price() {
System.out.println(" Rs 55000.00 ");
}
}
Step 5: Create a ShopKeeper concrete class that will use MobileShop interface.
public class ShopKeeper {
private MobileShop iphone;
private MobileShop samsung;
private MobileShop blackberry;
public ShopKeeper(){
iphone= new Iphone();
samsung=new Samsung();
blackberry=new Blackberry();
}
public void iphoneSale(){
iphone.modelNo();
iphone.price();
}
public void samsungSale(){
samsung.modelNo();
samsung.price();
}
public void blackberrySale(){
blackberry.modelNo();
blackberry.price();
}
}
Step 6: Now, Creating a client that can purchase the mobiles from MobileShop through ShopKeeper.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FacadePatternClient {
private static int choice;
public static void main(String args[]) throws NumberFormatException, IOException{
do{
System.out.print("========= Mobile Shop ============ \n");
System.out.print(" 1. IPHONE. \n");
System.out.print(" 2. SAMSUNG. \n");
System.out.print(" 3. BLACKBERRY. \n");
System.out.print(" 4. Exit. \n");
System.out.print("Enter your choice: ");
Name-Vikash kumar Section-4D3 Roll-11232947
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
choice=Integer.parseInt(br.readLine());
ShopKeeper sk=new ShopKeeper();
switch (choice) {
case 1:
{
sk.iphoneSale();
}
break;
case 2:
{
sk.samsungSale();
}
break;
case 3:
{
sk.blackberrySale();
}
break;
default:
{
System.out.println("Nothing You purchased");
}
return;
}
}
while(choice!=4);
}
}
OUTPUT-