Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is the difference between the Comparable pattern and the Strategy design pattern? Comparable pattern और Strategy design pattern में क्या अंतर है?

Answer

Comparable defines a class's single natural ordering baked into the class itself, while the Strategy pattern lets you define and SWAP entire algorithms (behaviors) at runtime, independent of the class using them.

// Comparable - one fixed, natural ordering built into the class
class Employee implements Comparable<Employee> {
    String name;
    double salary;
    Employee(String name, double salary) { this.name = name; this.salary = salary; }

    public int compareTo(Employee other) {
        return Double.compare(this.salary, other.salary);  // fixed: always sorts by salary
    }
}

// Strategy pattern - interchangeable algorithms via an interface
interface PaymentStrategy {
    void pay(double amount);
}

class CreditCardPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println('Paid ' + amount + ' via Credit Card');
    }
}

class PayPalPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println('Paid ' + amount + ' via PayPal');
    }
}

class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    // Strategy can be swapped at RUNTIME, unlike Comparable's fixed ordering
    public void setPaymentStrategy(PaymentStrategy strategy) {
        this.paymentStrategy = strategy;
    }

    public void checkout(double amount) {
        paymentStrategy.pay(amount);
    }
}

ShoppingCart cart = new ShoppingCart();
cart.setPaymentStrategy(new CreditCardPayment());
cart.checkout(100);  // Paid 100 via Credit Card

cart.setPaymentStrategy(new PayPalPayment());  // swap the algorithm entirely
cart.checkout(50);   // Paid 50 via PayPal

// Comparator is actually a real-world application of the Strategy pattern -
// it lets you swap the 'sorting algorithm/criteria' without touching the class:
List<Employee> employees = new ArrayList<>();
employees.sort(Comparator.comparing(e -> e.name));   // one strategy
employees.sort(Comparator.comparingDouble(e -> e.salary));  // a different strategy

Comparable class में built-in एक fixed natural ordering define करता है, जबकि Strategy pattern पूरे algorithms (behaviors) को runtime पर define और swap करने देता है, class से independent।

// Comparable - class में built-in एक fixed ordering
class Employee implements Comparable<Employee> {
    String name;
    double salary;
    Employee(String name, double salary) { this.name = name; this.salary = salary; }

    public int compareTo(Employee other) {
        return Double.compare(this.salary, other.salary);
    }
}

// Strategy pattern - interface से interchangeable algorithms
interface PaymentStrategy {
    void pay(double amount);
}

class CreditCardPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println('Paid ' + amount + ' via Credit Card');
    }
}

class PayPalPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println('Paid ' + amount + ' via PayPal');
    }
}

class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy strategy) {
        this.paymentStrategy = strategy;
    }

    public void checkout(double amount) {
        paymentStrategy.pay(amount);
    }
}

ShoppingCart cart = new ShoppingCart();
cart.setPaymentStrategy(new CreditCardPayment());
cart.checkout(100);

cart.setPaymentStrategy(new PayPalPayment());
cart.checkout(50);

// Comparator असल में Strategy pattern का real-world उदाहरण है
List<Employee> employees = new ArrayList<>();
employees.sort(Comparator.comparing(e -> e.name));
employees.sort(Comparator.comparingDouble(e -> e.salary));

Was this answer clear?