Dependency inversion is a principle in software design that promotes decoupling software modules. It encourages us to depend on abstractions (interfaces) rather than concrete implementations. This is part of the SOLID principles of object-oriented design.
<?php
interface PaymentProcessor {
public function processPayment($amount);
}
class PaypalProcessor implements PaymentProcessor {
public function processPayment($amount) {
// Logic to process payment through PayPal
echo "Processing payment of $$amount through PayPal.";
}
}
class StripeProcessor implements PaymentProcessor {
public function processPayment($amount) {
// Logic to process payment through Stripe
echo "Processing payment of $$amount through Stripe.";
}
}
class ShoppingCart {
private $paymentProcessor;
public function __construct(PaymentProcessor $processor) {
$this->paymentProcessor = $processor;
}
public function checkout($amount) {
$this->paymentProcessor->processPayment($amount);
}
}
// Usage
$cart = new ShoppingCart(new PaypalProcessor());
$cart->checkout(100); // Outputs: Processing payment of $100 through PayPal.
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?