Giter Club home page Giter Club logo

adapterdesignpattern's Introduction

Adapter design pattern

Adapter design pattern is a structural design pattern which enables two objects with incompatible interfaces works together. It converts interface of one object to another objects interface. The main components for adapter design patterns are

  1. Target It defines the interface that client expect to interact
  2. Adapter Adapter is a class that convert interface client expecting to be interact to interface adaptee need to work
  3. Adaptee Adaptee is the legacy class which has interface that incompatible with client's interface

This design pattern helps when we have legacy application or code from other teams that we can not modify.

Implementation of adapter design pattern

Suppose we are working in an e-commerce plateform and we need to process payements using multiple payment gateways like PayPal and Stripe. Each payment gateway has its own interface, to provide a unified interface we can use adapter design pattern

  1. Target - there is common interface IPaymentProcessor
namespace AdapterDesignPattern
{
    /// <summary>
    /// Target : target component defines unified interface for client.
    /// </summary>
    public interface IPaymentProcessor
    {
        void ProcessPayments(double amnt);
    }
}
  1. Adapter - Classes that change one interface to another, here we have PayPalPaymentGatewayAdapter & StripePaymentGatewayAdapter which converts IProcessPayment interface to gateways interfaces PayPalPaymentGatewayAdapter.cs
namespace AdapterDesignPattern
{
    /// <summary>
    /// Adapter: PayPal gateway adapter, it will convert client interface to PayPal's interface
    /// </summary>
    public class PayPalPaymentGatewayAdapter : IPaymentProcessor
    {
        private readonly PayPalPaymentGateway payPalPaymentGateway;

        /// <summary>
        /// Create instance for <see cref="PayPalPaymentGateway"/>
        /// </summary>
        /// <param name="payPalPaymentGateway"></param>
        public PayPalPaymentGatewayAdapter(PayPalPaymentGateway payPalPaymentGateway)
        {
            this.payPalPaymentGateway = payPalPaymentGateway;
        }

        /// <summary>
        /// Process payments
        /// </summary>
        /// <param name="amnt"></param>
        public void ProcessPayments(double amnt)
        {
            this.payPalPaymentGateway.MakePayment(amnt);
        }
    }
}

StripePaymentGatewayAdapter.cs

namespace AdapterDesignPattern
{
    /// <summary>
    /// Adapter: adapter for Stripe payment gateway that convert target interface to interface supported by Stripe
    /// </summary>
    public class StripePaymentGatewayAdapter : IPaymentProcessor
    {
        private readonly StripePaymentGateway stripePaymentGateway;

        /// <summary>
        /// Initialize instance for <see cref="StripePaymentGatewayAdapter"/>
        /// </summary>
        /// <param name="stripePaymentGateway"></param>
        public StripePaymentGatewayAdapter(StripePaymentGateway stripePaymentGateway)
        {
            this.stripePaymentGateway = stripePaymentGateway;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="amnt"></param>
        public void ProcessPayments(double amnt)
        {
            this.stripePaymentGateway.Change(amnt);
        }
    }
}
  1. Adaptee - legacy or third party code that is not easy to change and available with incompatible interfaces PayPalPaymentGateway.cs
namespace AdapterDesignPattern
{
    /// <summary>
    /// Adaptee: PayPal payment gateway which requires interface different from client 
    /// </summary>
    public class PayPalPaymentGateway
    {
        /// <summary>
        /// Method to make payments
        /// </summary>
        /// <param name="amount"></param>
        public void MakePayment(double amount)
        {
            Console.WriteLine($"Payment of amount {amount} processed successfully.");
        }
    }
}

StripePaymentGateway.cs

namespace AdapterDesignPattern
{
    /// <summary>
    /// Adaptee: Stripe payment gateway which requires interface different from client 
    /// </summary>
    public class StripePaymentGateway
    {
        /// <summary>
        /// Method to make payments
        /// </summary>
        /// <param name="amount"></param>
        public void Change(double amount)
        {
            Console.WriteLine($"Payment of amount {amount} processed successfully by Stripe payment gateway.");
        }
    }
}

adapterdesignpattern's People

Contributors

surender1987 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.