Skip to content

Instantly share code, notes, and snippets.

@devrath
Created May 15, 2022 17:48
Show Gist options
  • Select an option

  • Save devrath/531f53eab4003e44ba3f0e7d70573816 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/531f53eab4003e44ba3f0e7d70573816 to your computer and use it in GitHub Desktop.
public interface Element {
Boolean accept(Visitor visitor);
}
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;
}
}
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");
}
}
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;
}
}
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;
}
}
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;
}
}
public class PaymentGooglePay implements Element {
@Override
public Boolean accept(Visitor visitor) {
return visitor.visit(this);
}
}
public class PaymentPhonePe implements Element {
@Override
public Boolean accept(Visitor visitor) {
return visitor.visit(this);
}
}
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