Created
March 31, 2024 01:24
-
-
Save tangentlin/f38f520a5621e5580fb67d6549fa7002 to your computer and use it in GitHub Desktop.
A contrived declarative approach of declarative programming
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
interface PaymentMethod { | |
process(amount: number): void; | |
} | |
const paymentMethods: Record<string, PaymentMethod> = { | |
credit: { | |
process(amount: number) { | |
console.log(`Processing $${amount} via Credit Card`); | |
// Credit card processing logic... | |
} | |
}, | |
paypal: { | |
process(amount: number) { | |
console.log(`Processing $${amount} via PayPal`); | |
// PayPal processing logic... | |
} | |
}, | |
bank_transfer: { | |
process(amount: number) { | |
console.log(`Processing $${amount} via Bank Transfer`); | |
// Bank transfer processing logic... | |
} | |
} | |
// Add more methods as needed... | |
}; | |
function processPayment(amount: number, method: string) { | |
const processor = paymentMethods[method]; | |
if (!processor) { | |
throw new Error('Unsupported payment method'); | |
} | |
processor.process(amount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment