Created
November 8, 2022 16:44
-
-
Save rohanjai777/08863e1a9aba509554a7dae7eb4f7175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PaymentApi{ //Iterface for payments apis | |
boolean startTransaction(int transactionId); | |
boolean checkCompletion(int transactionId); | |
} | |
//--------------------- | |
//Make individual adapters to connect | |
class PayUAdapter implements PaymentApi{ //Adapter to connect | |
PayUApi api = new PayUApi(); | |
public boolean startTransaction(int transactionId){ | |
System.out.println("Transaction started using PayU "+transactionId); | |
return api.start(transactionId); | |
} | |
public boolean checkCompletion(int transactionId){ | |
System.out.println("Completion status of PayU "+transactionId); | |
return api.getStatus(transactionId); | |
} | |
} | |
class PaytmAdapter implements PaymentApi{ //Adapter to connect | |
PaytmApi api = new PaytmApi(); | |
public boolean startTransaction(int transactionId){ | |
System.out.println("Transaction started using PayTm "+transactionId); | |
return api.begin(transactionId); | |
} | |
public boolean checkCompletion(int transactionId){ | |
System.out.println("Completion status of PayTm "+transactionId); | |
return api.checkStatus(transactionId); | |
} | |
} | |
//------------------------ | |
//These are the APIs provided by Payment Company which have there own methods | |
class PayUApi{ | |
public boolean start(int id){ | |
//api runs business logic | |
return true; | |
} | |
public boolean getStatus(int id){ | |
//api runs business logic | |
return true; | |
} | |
} | |
class PaytmApi{ | |
public boolean begin(int id){ | |
//api runs business logic | |
return true; | |
} | |
public boolean checkStatus(int id){ | |
//api runs business logic | |
return true; | |
} | |
} | |
//------------------------ | |
public class Flipkart{ | |
static PaymentApi api; //initialize with payment api to use | |
public Flipkart(){ | |
api = new PaytmAdapter(); | |
} | |
public static void main(String[] args){ | |
Flipkart flipkart = new Flipkart(); //make object of flipkart | |
api.startTransaction(1); //common functions using adapter class | |
api.checkCompletion(1); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment