0% found this document useful (0 votes)
2 views4 pages

Chapter 9 The Strategy Pattern

Chapter 9 discusses the Strategy Pattern in software development, which addresses the challenge of handling multiple behaviors by encapsulating them in independent classes instead of using conditional statements. This pattern enhances flexibility, scalability, and maintainability, allowing for dynamic selection of algorithms at runtime. It is applicable in various real-world scenarios such as payment systems, navigation apps, and sorting algorithms, promoting clean code and easier extensibility.

Uploaded by

lilanidiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Chapter 9 The Strategy Pattern

Chapter 9 discusses the Strategy Pattern in software development, which addresses the challenge of handling multiple behaviors by encapsulating them in independent classes instead of using conditional statements. This pattern enhances flexibility, scalability, and maintainability, allowing for dynamic selection of algorithms at runtime. It is applicable in various real-world scenarios such as payment systems, navigation apps, and sorting algorithms, promoting clean code and easier extensibility.

Uploaded by

lilanidiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 9

The Strategy Pattern: Encapsulating Changing Behavior

9.1 Introduction
In software development, one of the most common challenges is handling situations where a
system must perform a task in multiple different ways. For example, a payment system may
support credit card, bank transfer, and digital wallet payments. A sorting system may apply
different sorting algorithms depending on data size. A navigation system may calculate routes
based on time, distance, or traffic conditions.

A common mistake in such scenarios is to place all possible behaviors inside a single class using
conditional statements such as if-else or switch. While this may work initially, it leads to
complex and rigid code as the number of behaviors increases.

The Strategy Pattern provides a clean solution to this problem by separating different
algorithms or behaviors into independent classes and allowing them to be selected dynamically
at runtime.

This pattern promotes flexibility, scalability, and maintainability by replacing conditional logic
with composition.

9.2 The Design Problem: Conditional Complexity


Consider a payment system where users can pay using different methods.

A simple implementation might look like this:

public class PaymentProcessor {

public void pay(String type, double amount) {

if ([Link]("CreditCard")) {
[Link]("Processing credit card payment of " +
amount);
} else if ([Link]("PayPal")) {
[Link]("Processing PayPal payment of " + amount);
} else if ([Link]("BankTransfer")) {
[Link]("Processing bank transfer of " + amount);
}
}
}
9.3 Problems with This Design
This design introduces several issues:

• Adding a new payment method requires modifying existing code


• Violates Open–Closed Principle
• Code becomes difficult to read and maintain
• Testing individual behaviors is difficult
• Business logic is tightly coupled

As systems grow, such conditional structures become unmanageable.

9.4 Real-World Analogy: Navigation Apps


Consider a navigation app like Google Maps. When a user searches for a route, they can choose:

• Fastest route
• Shortest route
• Avoid tolls

Each option represents a different strategy for route calculation. The app does not change its core
structure; it simply switches the strategy.

9.5 Understanding the Strategy Pattern


The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them
interchangeable at runtime.

Intent

To define multiple behaviors and allow them to be selected dynamically without modifying
existing code.

9.6 Structure of the Pattern


The pattern consists of:

• Strategy Interface
• Concrete Strategies
• Context Class
9.7 Applying the Strategy Pattern
Step 1: Create Strategy Interface
public interface PaymentStrategy {
void pay(double amount);
}

Step 2: Implement Concrete Strategies


public class CreditCardPayment implements PaymentStrategy {
public void pay(double amount) {
[Link]("Paid " + amount + " using Credit Card");
}
}

public class PayPalPayment implements PaymentStrategy {


public void pay(double amount) {
[Link]("Paid " + amount + " using PayPal");
}
}

public class BankTransferPayment implements PaymentStrategy {


public void pay(double amount) {
[Link]("Paid " + amount + " using Bank Transfer");
}
}

Step 3: Create Context Class


public class PaymentProcessor {

private PaymentStrategy strategy;

public void setStrategy(PaymentStrategy strategy) {


[Link] = strategy;
}

public void processPayment(double amount) {


[Link](amount);
}
}

Step 4: Client Code


public class Main {
public static void main(String[] args) {

PaymentProcessor processor = new PaymentProcessor();

[Link](new CreditCardPayment());
[Link](5000);
[Link](new PayPalPayment());
[Link](3000);
}
}

9.8 Explanation of Improved Design


Now the system does not depend on conditional logic. Each payment method is independent.
New strategies can be added without modifying existing classes.

This design promotes:

• Loose coupling
• Easy extensibility
• Clean architecture
• Reusability

9.9 Real-World Applications


The Strategy Pattern is used in:

• Payment gateways
• Sorting algorithms
• Compression tools
• Game AI behaviors
• Route planning systems

9.10 Advantages
• Eliminates conditional statements
• Easy to add new behaviors
• Promotes clean code
• Improves testing

9.11 Limitations
• Increases number of classes
• Requires understanding of design patterns
• Slight overhead in managing objects

You might also like