Please use Chain of Responsibility pattern for the corresponding code using this
ID: 3826099 • Letter: P
Question
Please use Chain of Responsibility pattern for the corresponding code using this reference https://sourcemaking.com/design_patterns/chain_of_responsibility
class LoanApprover
{
protected LoanApprover nextApprover;
public void SetNextApprover(LoanApprover nextApprover)
{
this.nextApprover = nextApprover;
}
public virtual void ApproveLoan(Loan i);
}
class Manager : public LoanApprover
{
public void ApproveLoan(Loan i)
{
if (i.Amount <= 100000)
cout << "Loan amount of " << i.Amount << " approved by the Manager") << endl;
else
nextApprover.ApproveLoan(i);
}
}
class Director : public LoanApprover
{
public void ApproveLoan(Loan i)
{
if (i.Amount <= 250000)
cout << "Loan amount of " << i.Amount << " approved by the Director") << endl;
else
nextApprover.ApproveLoan(i);
}
}
class VicePresident : LoanApprover
{
public void ApproveLoan(Loan i)
{
cout << "Loan amount of " << i.Amount << " approved by the Vice President") << endl;
}
}
public class Loan
{
private int amount;
public int Amount
{
return amount;
}
public Loan(int amount)
{
this.amount = amount;
}
}
void main(int argc, char* argv[])
{
LoanApprover a = new Manager();
LoanApprover b = new Director();
LoanApprover c = new VicePresident();
a.SetNextApprover(b);
b.SetNextApprover(c);
a.ApproveLoan(new Loan(50000)); //this will be approved by the manager
a.ApproveLoan(new Loan(200000)); //this will be approved by the director
a.ApproveLoan(new Loan(500000)); //this will be approved by the vice president
}
Explanation / Answer
Before
01
The client is responsible for stepping through the “list” of Handler objects, and determining when the request has been handled.
02
1-busy 2-busy 3-busy 4-busy 1-busy 2-handled-1 1-busy 2-busy 3-handled-2 1-busy 2-busy 3-busy 4-handled-3 1-busy 2-busy 3-busy 4-busy 1-busy 2-busy 3-handled-4 1-busy 2-busy 3-handled-5 1-handled-6 1-busy 2-handled-7 1-busy 2-busy 3-busy 4-busy 1-busy 2-busy 3-busy 4-handled-8 1-busy 2-handled-9
After
03
The client submits each request to the “chain” abstraction and is decoupled from all subsequent processing.
04