Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions code/Behavioral Design Patterns/SUGERENCIA
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Define individual approver functions
const manager = (amount) => {
if (amount <= 1000) {
console.log(`Manager approves the purchase of $${amount}.`);
return true;
}
return false;
};

const director = (amount) => {
if (amount <= 5000) {
console.log(`Director approves the purchase of $${amount}.`);
return true;
}
return false;
};

const vicePresident = (amount) => {
if (amount <= 10000) {
console.log(`Vice President approves the purchase of $${amount}.`);
return true;
}
return false;
};

// Set up the chain of responsibility
const chainOfResponsibility = [manager, director, vicePresident];

// Client
const processRequest = (amount) => {
for (const approver of chainOfResponsibility) {
if (approver(amount)) {
return;
}
}
console.log("None of the approvers can handle the request.");
};

// Test the chain with different purchase amounts
processRequest(800); // Manager approves the purchase of $800
processRequest(4500); // Director approves the purchase of $4500
processRequest(10000); // Vice President approves the purchase of $10000
processRequest(15000); // None of the approvers can handle the request.