Last active
July 27, 2021 08:00
-
-
Save DhavalDalal/5f77d027d0a7be137cdb605097483e28 to your computer and use it in GitHub Desktop.
Bank Maker-Checker Refactoring (Java)
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
package bank; | |
public interface ApprovalStrategy { | |
boolean approve(Transactions ts); | |
public static ApprovalStrategy valueOf(Transactions transactions) { | |
if (transactions.totalValue() < 100000d) | |
return new SingleMakerChecker(new Maker(), new Checker()); | |
else | |
return new DoubleMakerChecker(new Maker(), new Checker(), new Checker()); | |
} | |
} |
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
package bank; | |
class Checker { } |
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
package bank; | |
class DoubleMakerChecker implements ApprovalStrategy { | |
public DoubleMakerChecker(Maker m, Checker c1, Checker c2) { } | |
public boolean approve(Transactions ts) { | |
return true; | |
} | |
} |
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
package bank; | |
import java.util.List; | |
public class Main { | |
public static List<Transaction> createTransactions() { | |
Transaction t1 = new Transaction() { | |
public boolean approve() { return true; } | |
public boolean reject(String reason) { return false; } | |
}; | |
return Arrays.asList(t1, t1); | |
} | |
public static void main(String[] args) { | |
Transactions transactions = new Transactions(createTransactions()); | |
ApprovalStrategy approvalStrategy = ApprovalStrategy.valueOf(transactions); | |
transactions.approve(approvalStrategy); | |
} | |
} |
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
package bank; | |
class Maker { } |
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
package bank; | |
class SingleMakerChecker implements ApprovalStrategy { | |
public SingleMakerChecker(Maker m, Checker c) { } | |
public boolean approve(Transactions ts) { | |
return true; | |
} | |
} |
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
package bank; | |
public interface Transaction { | |
public boolean approve(); | |
public boolean reject(String reason); | |
} |
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
package bank; | |
import java.util.List; | |
public class Transactions { | |
private final List<Transaction> transactions; | |
public Transactions(List<Transaction> transactions) { | |
this.transactions = transactions; | |
} | |
public boolean approve(ApprovalStrategy aps) { | |
return aps.approve(this); | |
} | |
public Double totalValue() { | |
// This will loop through all transactions and add all transactions value. | |
// This is hard-coded for purpose of this example. | |
return 1000000d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment