DEV Community

Kittipat.po
Kittipat.po

Posted on

Understanding the Adapter Pattern in Go

Image description

Adapter Pattern

The Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to collaborate. Imagine trying to charge your laptop using a plug that doesn't fit the socket - an adapter solves this by making the connection possible. Similarly, in software development, the Adapter Pattern acts as a bridge between two incompatible interfaces, enabling them to work together seamlessly.

The Adapter Pattern in Action

Let's illustrate the Adapter Pattern with a real-world scenario: integrating a new payment processing system into an existing e-commerce platform written in Go. The current platform uses a payment service with a method signature that doesn't match the new payment processing system's API. To integrate this new system without altering the existing codebase significantly, we can use the Adapter Pattern.

Example: Integrating a New Payment System

Suppose our existing e-commerce platform uses a simple payment interface:



// Existing payment interface
type PaymentProcessor interface {
    ProcessPayment(amount float64) bool
}


Enter fullscreen mode Exit fullscreen mode

And the existing implementation:



// Existing payment service
type PayPalProcessor struct{}

func (p *PayPalProcessor) ProcessPayment(amount float64) bool {
    fmt.Println("Processing payment with PayPal:", amount)
    // Logic to process payment through PayPal
    return true // Assume payment is processed successfully
}


Enter fullscreen mode Exit fullscreen mode

Now, we want to integrate a new payment service, Stripe, which has a different method signature:



// New payment service we want to integrate
type StripeProcessor struct{}

func (s *StripeProcessor) ChargeCreditCard(name string, amount float64) bool {
    fmt.Println("Charging credit card through Stripe:", name, amount)
    // Logic to charge credit card through Stripe
    return true // Assume charge is successful
}


Enter fullscreen mode Exit fullscreen mode

Notice the mismatch in method signatures between PayPalProcessor and StripeProcessor. To integrate StripeProcessor without altering our existing code, we create an adapter:



// Adapter for StripeProcessor to implement PaymentProcessor interface
type StripeAdapter struct {
    Stripe *StripeProcessor
}

func (sa *StripeAdapter) ProcessPayment(amount float64) bool {
    // Adapter translates the method call to the format expected by Stripe
    return sa.Stripe.ChargeCreditCard("Customer Name", amount)
}


Enter fullscreen mode Exit fullscreen mode

With the adapter in place, our e-commerce platform can now use StripeProcessor as a PaymentProcessor, enabling seamless integration of the new payment service:



func main() {
// Existing system using PayPal
payPal := &PayPalProcessor{}
processPayment(payPal, 100.0)

<span class="c">// New system using Stripe through the adapter</span>
<span class="n">stripe</span> <span class="o">:=</span> <span class="o">&amp;</span><span class="n">StripeAdapter</span><span class="p">{</span><span class="n">Stripe</span><span class="o">:</span> <span class="o">&amp;</span><span class="n">StripeProcessor</span><span class="p">{}}</span>
<span class="n">processPayment</span><span class="p">(</span><span class="n">stripe</span><span class="p">,</span> <span class="m">100.0</span><span class="p">)</span>
Enter fullscreen mode Exit fullscreen mode

}

func processPayment(p processor, amount float64) {
success := p.ProcessPayment(amount)
if success {
fmt.Println("Payment processed successfully.")
} else {
fmt.Println("Payment processing failed.")
}
}

Enter fullscreen mode Exit fullscreen mode




Advantages of the Adapter Pattern

  • Flexibility and Reusability: The Adapter Pattern allows for existing classes to be reused even if their interfaces don't match the expected system requirements, promoting code reuse.
  • Simplified Integration: It simplifies the integration of third-party libraries or systems by acting as a bridge between the application's core code and external services.
  • Decoupled Code: The pattern helps in decoupling the client code from the implementation of an interface, making the system more modular and easier to refactor or extend.

Conclusion 🍻

The Adapter Pattern is a vital tool in the software developer's toolbox. It provides an elegant solution to interface incompatibility issues, allowing for the smooth integration of disparate systems without extensive modifications to existing code. By understanding and applying the Adapter Pattern, developers can enhance the flexibility, reusability, and maintainability of their Go applications, ensuring that they can adapt to new requirements with minimal friction.

Top comments (0)