Close Menu
    Latest Post

    Verifying 5G Standalone Activation on Your iPhone

    March 1, 2026

    Hands on: the Galaxy S26 and S26 Plus are more of the same for more money

    March 1, 2026

    IronCurtain: A Secure AI Agent Designed to Prevent Rogue Actions

    March 1, 2026
    Facebook X (Twitter) Instagram
    Trending
    • Verifying 5G Standalone Activation on Your iPhone
    • Hands on: the Galaxy S26 and S26 Plus are more of the same for more money
    • IronCurtain: A Secure AI Agent Designed to Prevent Rogue Actions
    • Kwasi Asare’s Entrepreneurial Journey: Risk, Reputation, and Resilience
    • The Rubin Observatory’s alert system sent 800,000 pings on its first night
    • GitHub Actions Now Supports Unzipped Artifact Uploads and Downloads
    • Project Genie: Experimenting with Infinite, Interactive Worlds
    • Text Generation Using Diffusion Models and ROI with LLMs
    Facebook X (Twitter) Instagram Pinterest Vimeo
    NodeTodayNodeToday
    • Home
    • AI
    • Dev
    • Guides
    • Products
    • Security
    • Startups
    • Tech
    • Tools
    NodeTodayNodeToday
    Home»Dev»LLD-2:ECommerces Checkout
    Dev

    LLD-2:ECommerces Checkout

    Samuel AlejandroBy Samuel AlejandroFebruary 13, 2026No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    src 1njsnu0 featured
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Image 2 Image 3 Image 4 Image 5 Image 6 

    E-Commerce Payment System – Strategy Pattern Implementation

    This article explores the implementation of the Strategy design pattern to manage various payment methods within an e-commerce checkout system.

    Problem Statement

    Developing a robust backend for online retail platforms, such as Flipkart or Amazon, necessitates supporting diverse payment options. Each payment method involves unique data requirements and processing steps. The challenge is to design a system that can easily accommodate new payment methods without resorting to complex conditional logic (if-else chains).

    Class Diagram

            +------------------+           +----------------------+
            |  PaymentRequest  |---------->|    PaymentFactory    |
            +------------------+           +----------------------+
            | - type: Enum     |           | + getStrategy(req)   |
            | - amount: double |           +----------+-----------+
            | - card/upi data  |                      |
            +------------------+                      | (Creates)
                                                      v
            +------------------+           +----------------------+
            |  PaymentService  |           |      PayStrategy     |
            |     (Context)    |           |      (Interface)     |
            +------------------+           +----------------------+
            | - strategy       |<>-------->| + pay(amount)        |
            | + makePayment()  |           +----------+-----------+
            +------------------+                      ^
                                                      | (Implements)
                                                      |
                       +---------------------+--------+--------+----------------------+
                       |                     |                 |                      |
             +--------------------+ +-----------------+ +--------------------+
             | CreditCardStrategy | |   UPIStrategy   | |   PayPalStrategy   |
             +--------------------+ +-----------------+ +--------------------+
             | - cardNumber       | | - upiId         | | - email, password  |
             +--------------------+ +-----------------+ +--------------------+
    

    Implementation

    package ecommercecheckout;
    
    /**
     * Main Driver Class for the E-Commerce Payment System.
     * Implements Strategy Pattern (for algorithms) and Factory Pattern (for object creation).
     */
    public class ECommerceCheckout {
    
        /**
         * Enum to define supported payment types.
         * Ensures Type Safety and prevents invalid string inputs.
         */
        enum PaymentType {
            CREDIT_CARD,
            UPI,
            PAYPAL
        }
    
        /**
         * Data Transfer Object (DTO) for Payment Requests.
         * Acts as a unified form to collect all possible inputs from the user.
         */
        static class PaymentRequest {
            public PaymentType paymentType;
            public double amount;
    
            // Specific fields for different strategies
            public String cardNumber;
            public String cvv;
            public String expiry;
            public String upiId;
            public String email;
            public String password;
        }
    
        /**
         * Strategy Interface.
         * Defines the common contract that all payment methods must follow.
         */
        public interface PayStrategy {
            void pay(Double amount);
        }
    
        /**
         * Concrete Strategy for Credit Card.
         * Uses Constructor Injection to receive card details.
         */
        static class CreditCardStrategy implements PayStrategy {
            private final String cardNumber;
            private final String cvv;
            private final String expiryDate;
    
            public CreditCardStrategy(String cardNumber, String cvv, String expiryDate) {
                this.cardNumber = cardNumber;
                this.cvv = cvv;
                this.expiryDate = expiryDate;
            }
    
            @Override
            public void pay(Double amount) {
                System.out.println("Validation Card: " + this.cardNumber);
                System.out.println("Paid " + amount + " successfully");
                System.out.println("Card Details: Card Number => " + this.cardNumber + 
                                 " CVV => " + this.cvv + " ExpiryDate => " + expiryDate);
            }
        }
    
        /**
         * Concrete Strategy for UPI.
         * Encapsulates logic specific to UPI payments.
         */
        static class UPIStrategy implements PayStrategy {
            private final String upiId;
    
            public UPIStrategy(String upiId) {
                this.upiId = upiId;
            }
    
            @Override
            public void pay(Double amount) {
                System.out.println("Validating UPI Id: " + this.upiId);
                System.out.println("Paid " + amount + " successfully");
                System.out.println("UPI Details: UPI Id => " + this.upiId);
            }
        }
    
        /**
         * Concrete Strategy for PayPal.
         * Encapsulates logic specific to Email/Password authentication.
         */
        static class PayPalStrategy implements PayStrategy {
            private final String email;
            private final String password;
    
            public PayPalStrategy(String email, String password) {
                this.email = email;
                this.password = password;
            }
    
            @Override
            public void pay(Double amount) {
                System.out.println("Validating Email: " + this.email + 
                                 " and PassWord: " + this.password);
                System.out.println("Paid " + amount + " successfully");
                System.out.println("PayPal Detail: Email => " + this.email + 
                                 " Password => " + this.password);
            }
        }
    
        /**
         * Factory Class.
         * Centralizes the object creation logic, keeping the Client code clean.
         */
        static class PaymentFactory {
            /**
             * Creates and returns the appropriate Strategy object based on the Request Type.
             * @param request The input DTO containing payment details.
             * @return A valid PayStrategy instance.
             */
            public static PayStrategy getStrategy(PaymentRequest request) {
                switch (request.paymentType) {
                    case CREDIT_CARD -> {
                        return new CreditCardStrategy(request.cardNumber, 
                                                     request.cvv, request.expiry);
                    }
                    case UPI -> {
                        return new UPIStrategy(request.upiId);
                    }
                    case PAYPAL -> {
                        return new PayPalStrategy(request.email, request.password);
                    }
                    default -> {
                        throw new IllegalArgumentException("Unsupported Payment Type: " + 
                                                          request.paymentType);
                    }
                }
            }
        }
    
        /**
         * Context Class (The Shopping Cart).
         * Maintains a reference to the current Strategy and delegates execution.
         */
        static class PaymentService {
            private PayStrategy strategy;
    
            /**
             * Allows changing the payment strategy dynamically at runtime.
             */
            public void setStrategy(PayStrategy strategy) {
                this.strategy = strategy;
            }
    
            /**
             * Executes the payment using the currently selected strategy.
             */
            public void makePayment(Double amount) {
                if(strategy == null) {
                    System.out.println("Error: No Payment method selected");
                    return;
                }
    
                System.out.println("\n----Getting Payment Mode-----");
                strategy.pay(amount);
                System.out.println("-------------------------------");
            }
        }
    
        /**
         * Main method to simulate the User/Client interaction.
         */
        public static void main(String[] args) {
            PaymentService razorPay = new PaymentService();
    
            System.out.println("Simulation User: Shiv");
    
            // Scenario 1: Credit Card Payment
            try {
                PaymentRequest request = new PaymentRequest();
                request.paymentType = PaymentType.CREDIT_CARD;
                request.cardNumber = "123456";
                request.cvv = "111";
                request.expiry = "11-2";
    
                PayStrategy payStrategy = PaymentFactory.getStrategy(request);
                razorPay.setStrategy(payStrategy);
                razorPay.makePayment(100.00);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
            // Scenario 2: UPI Payment
            try {
                PaymentRequest request = new PaymentRequest();
                request.paymentType = PaymentType.UPI;
                request.upiId = "1234567890";
    
                PayStrategy payStrategy = PaymentFactory.getStrategy(request);
                razorPay.setStrategy(payStrategy);
                razorPay.makePayment(20.00);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
            // Scenario 3: PayPal Payment
            try {
                PaymentRequest request = new PaymentRequest();
                request.paymentType = PaymentType.PAYPAL;
                request.email = "[email protected]";
                request.password = "12345678";
    
                PayStrategy payStrategy = PaymentFactory.getStrategy(request);
                razorPay.setStrategy(payStrategy);
                razorPay.makePayment(50.00);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

    Key Features

    • Strategy Pattern: This pattern encapsulates distinct payment algorithms, allowing them to be interchangeable.
    • Factory Pattern: Object creation logic is centralized, providing a unified way to instantiate payment method objects.
    • Type Safety: The use of Enums ensures type safety, preventing errors that can arise from using raw strings for payment method identification.
    • Extensible: The design facilitates the straightforward addition of new payment methods without requiring modifications to existing codebase.
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous Article10 Ways to Fix Chrome’s “Out of Memory” Error Code
    Next Article Chinese Open-Source AI: What’s Next?
    Samuel Alejandro

    Related Posts

    Dev

    Text Generation Using Diffusion Models and ROI with LLMs

    March 1, 2026
    Dev

    RCCLX: Innovating GPU Communications on AMD Platforms

    February 28, 2026
    Dev

    RSC for LISP Developers

    February 26, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Latest Post

    ChatGPT Mobile App Surpasses $3 Billion in Consumer Spending

    December 21, 202517 Views

    Automate Your iPhone’s Always-On Display for Better Battery Life and Privacy

    December 21, 202515 Views

    Creator Tayla Cannon Lands $1.1M Investment for Rebuildr PT Software

    December 21, 202514 Views
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    About

    Welcome to NodeToday, your trusted source for the latest updates in Technology, Artificial Intelligence, and Innovation. We are dedicated to delivering accurate, timely, and insightful content that helps readers stay ahead in a fast-evolving digital world.

    At NodeToday, we cover everything from AI breakthroughs and emerging technologies to product launches, software tools, developer news, and practical guides. Our goal is to simplify complex topics and present them in a clear, engaging, and easy-to-understand way for tech enthusiasts, professionals, and beginners alike.

    Latest Post

    Verifying 5G Standalone Activation on Your iPhone

    March 1, 20264 Views

    Hands on: the Galaxy S26 and S26 Plus are more of the same for more money

    March 1, 20265 Views

    IronCurtain: A Secure AI Agent Designed to Prevent Rogue Actions

    March 1, 20264 Views
    Recent Posts
    • Verifying 5G Standalone Activation on Your iPhone
    • Hands on: the Galaxy S26 and S26 Plus are more of the same for more money
    • IronCurtain: A Secure AI Agent Designed to Prevent Rogue Actions
    • Kwasi Asare’s Entrepreneurial Journey: Risk, Reputation, and Resilience
    • The Rubin Observatory’s alert system sent 800,000 pings on its first night
    Facebook X (Twitter) Instagram Pinterest
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    • Disclaimer
    • Cookie Policy
    © 2026 NodeToday.

    Type above and press Enter to search. Press Esc to cancel.