Implementing Interfaces

Implementing Interfaces
πŸ‘¨β€πŸ’Ό Perfect! Both CreditCard and PayPal implement the same interface, ensuring they can be used interchangeably.
πŸ¦‰ The interface defines the contract (pay() method), and each class provides its own implementation. This allows different payment methods to work through the same interface.

Interfaces: Classes vs Functions

Interfaces can define contracts for both classes and functions:
// OOP approach - interface with classes
interface PaymentProcessor {
	process(amount: number): boolean
}
class StripeProcessor implements PaymentProcessor {
	process(amount: number) {
		return true
	}
}

// FP approach - function type
type PaymentProcessor = (amount: number) => boolean
const stripeProcessor: PaymentProcessor = (amount) => true
Both approaches achieve polymorphismβ€”just through different mechanisms. The OOP approach bundles state and behavior; the FP approach keeps functions separate from data.

Please set the playground first

Loading "Implementing Interfaces"
Loading "Implementing Interfaces"