Skip to content

Instantly share code, notes, and snippets.

@tangentlin
Created March 31, 2024 01:24
Show Gist options
  • Save tangentlin/f38f520a5621e5580fb67d6549fa7002 to your computer and use it in GitHub Desktop.
Save tangentlin/f38f520a5621e5580fb67d6549fa7002 to your computer and use it in GitHub Desktop.
A contrived declarative approach of declarative programming
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