Created
May 15, 2022 17:48
-
-
Save devrath/531f53eab4003e44ba3f0e7d70573816 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public interface Element { | |
| Boolean accept(Visitor visitor); | |
| } |
This file contains hidden or 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
| public class FeaturePayment { | |
| // This will store the list of operations to be performed | |
| private List<Element> operations = new ArrayList<>(); | |
| // Adding the operation to the list of operations | |
| public void add(Element element) { | |
| operations.add(element); | |
| } | |
| // Initiating the list of operations one after other | |
| public boolean execute(Visitor visitor){ | |
| boolean areOperationsSuccessful = true; | |
| for (Element element : operations) { | |
| areOperationsSuccessful = element.accept(visitor); | |
| if(areOperationsSuccessful==false){ | |
| break; | |
| } | |
| } | |
| return areOperationsSuccessful; | |
| } | |
| } |
This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| System.out.println("Starting the Program"); | |
| // ------------------------------------------------------------ | |
| FeaturePayment feat = new FeaturePayment(); | |
| // Feature to be executed | |
| feat.add(new PaymentGooglePay()); | |
| // Operations to be performed | |
| boolean resultIsBlocked = feat.execute(new OperationIsUserBlocked()); | |
| boolean resultIsValidTransaction = feat.execute(new OperationIsValidTransaction()); | |
| boolean resultHasCardSaved = feat.execute(new OperationHasCardSaved()); | |
| if (resultIsBlocked && resultIsValidTransaction && resultHasCardSaved) { | |
| System.out.println("Operation successful"); | |
| } else { | |
| System.out.println("Operation un-successful"); | |
| } | |
| // ------------------------------------------------------------ | |
| System.out.println("Ending the Program"); | |
| } | |
| } |
This file contains hidden or 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
| public class OperationHasCardSaved implements Visitor { | |
| @Override | |
| public Boolean visit(PaymentGooglePay googlepay) { | |
| System.out.println("Implementation - checking google pay user credentials is saved or not"); | |
| return true; | |
| } | |
| @Override | |
| public Boolean visit(PaymentPhonePe paymentPhonePe) { | |
| System.out.println("Implementation - checking phone pay user credentials is saved or not"); | |
| return true; | |
| } | |
| } |
This file contains hidden or 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
| public class OperationIsUserBlocked implements Visitor { | |
| @Override | |
| public Boolean visit(PaymentGooglePay googlepay) { | |
| System.out.println("Implementation - checking google pay user is blocked or not"); | |
| return false; | |
| } | |
| @Override | |
| public Boolean visit(PaymentPhonePe paymentPhonePe) { | |
| System.out.println("Implementation - checking phone pay user is blocked or not"); | |
| return true; | |
| } | |
| } |
This file contains hidden or 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
| public class OperationIsValidTransaction implements Visitor { | |
| @Override | |
| public Boolean visit(PaymentGooglePay googlePay) { | |
| System.out.println("Implementation - checking google pay transaction is valid ot not"); | |
| return true; | |
| } | |
| @Override | |
| public Boolean visit(PaymentPhonePe paymentPhonePe) { | |
| System.out.println("Implementation - checking phone pay transaction is valid ot not"); | |
| return true; | |
| } | |
| } |
This file contains hidden or 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
| public class PaymentGooglePay implements Element { | |
| @Override | |
| public Boolean accept(Visitor visitor) { | |
| return visitor.visit(this); | |
| } | |
| } |
This file contains hidden or 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
| public class PaymentPhonePe implements Element { | |
| @Override | |
| public Boolean accept(Visitor visitor) { | |
| return visitor.visit(this); | |
| } | |
| } |
This file contains hidden or 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
| public interface Visitor { | |
| Boolean visit(PaymentGooglePay googlePay); | |
| Boolean visit(PaymentPhonePe paymentPhonePe); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment