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

Strategy Pattern Explained with Examples

Uploaded by

Natalia B.
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)
4 views4 pages

Strategy Pattern Explained with Examples

Uploaded by

Natalia B.
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

15/01/24, 11:44 Design Patterns - Strategy Pattern

Design Patterns - Strategy Pattern

In Strategy pattern, a class behavior or its algorithm can be changed at run time.
This type of design pattern comes under behavior pattern.

In Strategy pattern, we create objects which represent various strategies and a


context object whose behavior varies as per its strategy object. The strategy object
changes the executing algorithm of the context object.

Implementation
We are going to create a Strategy interface defining an action and concrete strategy
classes implementing the Strategy interface. Context is a class which uses a
Strategy.

StrategyPatternDemo, our demo class, will use Context and strategy objects to
demonstrate change in Context behaviour based on strategy it deploys or uses.

Step 1
Create an interface.

[Link]

public interface Strategy {


public int doOperation(int num1, int num2);
[Link] 1/4
15/01/24, 11:44 Design Patterns - Strategy Pattern

Step 2
Create concrete classes implementing the same interface.

[Link]

public class OperationAdd implements Strategy{


@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}

[Link]

public class OperationSubstract implements Strategy{


@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}

[Link]

public class OperationMultiply implements Strategy{


@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}

Step 3
Create Context Class.

[Link]

[Link] 2/4
15/01/24, 11:44 Design Patterns - Strategy Pattern

public class Context {


private Strategy strategy;

public Context(Strategy strategy){


[Link] = strategy;
}

public int executeStrategy(int num1, int num2){


return [Link](num1, num2);
}
}

Step 4
Use the Context to see change in behaviour when it changes its Strategy.

[Link]

public class StrategyPatternDemo {


public static void main(String[] args) {
Context context = new Context(new OperationAdd());
[Link]("10 + 5 = " + [Link](10, 5))

context = new Context(new OperationSubstract());


[Link]("10 - 5 = " + [Link](10, 5))

context = new Context(new OperationMultiply());


[Link]("10 * 5 = " + [Link](10, 5))
}
}

Step 5
Verify the output.

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50

[Link] 3/4
15/01/24, 11:44 Design Patterns - Strategy Pattern

[Link] 4/4

Common questions

Powered by AI

Comparing the Strategy pattern with the State pattern, both involve changing the behavior of an object based on different classes. However, the Strategy pattern focuses on defining a set of interchangeable algorithms that the context can switch between at runtime, targeted at altering behavior based on a strategy interface. In contrast, the State pattern associates behavior changes with state changes of the object it is managing, encapsulating state-dependent behavior in state-specific classes. The State pattern often leads to fewer classes than the Strategy pattern due to directly mapping states to behaviors, while Strategy aims at algorithm swapping .

The steps to implement the Strategy pattern include: creating an interface (Strategy) that encapsulates a family of algorithms, implementing concrete strategy classes adhering to this interface, creating a context class that uses a strategy object, and using a client or demo class to change the context’s behavior by assigning different strategy objects. These steps facilitate the dynamic quality of the Strategy pattern by promoting algorithm interchangeability, enhancing the context's ability to adapt to different conditions without modifying its code, thereby increasing system flexibility .

The Strategy pattern improves code maintainability and flexibility by promoting separation of concerns and encapsulating each algorithm within its own class. This makes it easy to extend and modify the code by simply adding new strategy classes. It also allows for the reuse of algorithms across different contexts. This modular approach helps to avoid tight coupling, reduces the risk of code duplication, and makes the system more scalable. Furthermore, changes to an algorithm do not affect the context or other algorithms, thus preserving code stability .

A beneficial scenario for using the Strategy pattern would be in a tax calculation system where different tax strategies are applied based on the client's region. By applying the Strategy pattern, each strategy class performs specific tax calculations (e.g., VAT, GST, income tax), allowing the context class to dynamically assign and execute the appropriate tax calculation strategy based on client input. This approach would simplify adding new regional tax strategies without altering the existing system structure, providing flexibility to adapt to regulatory changes efficiently .

Implementing the Strategy pattern can impact system performance due to overhead from creating additional objects for each strategy, particularly in systems where strategies are frequently created and discarded. This can lead to increased memory usage and object churn, potentially taxing garbage collection. However, if these strategies are reused over the application's lifetime, the impact may be mitigated. Careful management of strategy instances and considering pooling or caching strategies may be necessary to optimize resource use .

The Strategy interface defines an action that all concrete strategy classes must implement. It is critical because it provides the structure and contract that each strategy must adhere to, ensuring that the context can interchangeably utilize any strategy implementation that conforms to this interface. This abstraction allows for easy addition of new strategies without altering the context itself .

The Strategy pattern adheres to the "Open/Closed Principle" by allowing classes to be open for extension but closed for modification. New strategies can be added by creating new classes that implement the Strategy interface without altering existing code in the context or in other strategies. This extensibility means the system can accommodate new behavioral strategies with minimal impact, leveraging polymorphism to integrate them seamlessly without modifying the context, thus preserving existing functionality .

The Strategy pattern allows a class's behavior to be dynamically changed at runtime by using objects that represent different strategies. The context class holds a reference to a strategy object, and its behavior can change by altering this strategy object. By changing the strategy object associated with the context, the executing algorithm is modified without changing the code that uses the context. This flexibility facilitates an interchangeable strategy execution as needed .

The potential drawbacks of using the Strategy pattern include increased complexity due to the number of classes introduced for each strategy, which can make the system harder to understand and manage. It may also lead to overhead because each strategy is instantiated as an object, potentially impacting performance if many strategies are used frequently. Additionally, if strategies have to share state or data, the pattern can become cumbersome as it is not originally designed to handle shared data among strategies easily .

The Strategy pattern reflects the dependency inversion principle by ensuring that high-level modules (context class) do not depend on lower-level modules (specific strategy implementations). Instead, both rely on abstractions provided by the Strategy interface. This design allows the context to use any of the concrete strategy classes without being affected by changes in those implementations, as they all adhere to the same interface. This separation enhances decoupling and maintains a flexible architecture .

You might also like