Skip to content

Create SG.ts #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
73 changes: 73 additions & 0 deletions code/Behavioral Design Patterns/SG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Handler interface
interface Approver {
setNext(nextApprover: Approver): Approver;
processRequest(amount: number): void;
}

// Base Handler class implementing the Approver interface
abstract class AbstractApprover implements Approver {
private nextApprover: Approver | null = null;

setNext(nextApprover: Approver): Approver {
this.nextApprover = nextApprover;
return nextApprover;
}

processRequest(amount: number): void {
if (this.canApprove(amount)) {
this.approve(amount);
} else if (this.nextApprover) {
this.nextApprover.processRequest(amount);
}
}

protected abstract canApprove(amount: number): boolean;
protected abstract approve(amount: number): void;
}

// Concrete Handler 1
class Manager extends AbstractApprover {
protected canApprove(amount: number): boolean {
return amount <= 1000;
}

protected approve(amount: number): void {
console.log(`Manager approves the purchase of $${amount}.`);
}
}

// Concrete Handler 2
class Director extends AbstractApprover {
protected canApprove(amount: number): boolean {
return amount <= 5000;
}

protected approve(amount: number): void {
console.log(`Director approves the purchase of $${amount}.`);
}
}

// Concrete Handler 3
class VicePresident extends AbstractApprover {
protected canApprove(amount: number): boolean {
return amount <= 10000;
}

protected approve(amount: number): void {
console.log(`Vice President approves the purchase of $${amount}.`);
}
}

// Client
const manager = new Manager();
const director = new Director();
const vicePresident = new VicePresident();

// Set up the chain of responsibility
manager.setNext(director).setNext(vicePresident);

// Test the chain with different purchase amounts
manager.processRequest(800); // Manager approves the purchase of $800
manager.processRequest(4500); // Director approves the purchase of $4500
manager.processRequest(10000); // Vice President approves the purchase of $10000