Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Created May 15, 2019 11:32
Show Gist options
  • Save DhavalDalal/e6161a012a584387fbcfe67fc9a56054 to your computer and use it in GitHub Desktop.
Save DhavalDalal/e6161a012a584387fbcfe67fc9a56054 to your computer and use it in GitHub Desktop.
Bank Maker-Checker Refactoring (C#)

Bank Maker-Checker Refactoring (C#)

namespace Bank {
public abstract class ApprovalStrategy {
public abstract bool Approve(Transactions transactions);
public static ApprovalStrategy ValueOf(Transactions transactions) {
if (transactions.TotalValue() < 100000)
return new SingleMakerChecker(new Maker(), new Checker());
else
return new DoubleMakerChecker(new Maker(),
new Checker(), new Checker());
}
}
public class SingleMakerChecker : ApprovalStrategy {
public SingleMakerChecker(Maker m, Checker c) { }
public override bool Approve(Transactions ts) {
return true;
}
}
public class DoubleMakerChecker : ApprovalStrategy {
public DoubleMakerChecker(Maker m, Checker c1, Checker c2) { }
public override bool Approve(Transactions ts) {
return true;
}
}
}
namespace Bank {
public class Maker { }
public class Checker { }
public interface Transaction {
bool Approve();
bool Reject(String reason);
}
public class Debit : Transaction {
public bool Approve() { return true; }
public bool Reject(String reason) { return false; }
}
public class Credit : Transaction {
public bool Approve() { return true; }
public bool Reject(String reason) { return false; }
}
public class Transactions {
private List<Transaction> transactions;
public Transactions(List<Transaction> transactions) {
this.transactions = transactions;
}
public bool Approve(ApprovalStrategy aps) {
return aps.Approve(this);
}
public double TotalValue() {
// This is hard-coded for purpose of this example.
return 1000000d;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using Bank;
public class Main {
public static List<Transaction> CreateTransactions() {
return new List<Transaction> { new Debit(), new Credit() };
}
public static void Main(String[] args) {
Transactions transactions = new Transactions(CreateTransactions());
ApprovalStrategy approvalStrategy = ApprovalStrategy.ValueOf(transactions);
Console.WriteLine(transactions.Approve(approvalStrategy)); // True
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment