What is the intent of Strategy pattern?

What is the intent of Strategy pattern?

Intent. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. Capture the abstraction in an interface, bury implementation details in derived classes.

Can strategy pattern have multiple methods?

1 Answer. No you can have more than one method on your strategy interface. However, in order for your strategy object to actually use the Strategy pattern, at least one of the method implementations should differ between the different strategies.

How are design patterns related to 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.

How is the strategy pattern used in Java?

The Strategy pattern lets you extract the varying behavior into a separate class hierarchy and combine the original classes into one, thereby reducing duplicate code. Use the pattern to isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.

How are algorithms interchangeable in the strategy pattern?

The algorithms are interchangeable, meaning that they are substitutable for each other. The strategy pattern is a behavioral design pattern that enables selecting an algorithm at runtime — Wikipedia The key idea is to create objects which represent various strategies.

When to use a pattern in a class?

Use the pattern when your class has a massive conditional operator that switches between different variants of the same algorithm. The Strategy pattern lets you do away with such a conditional by extracting all algorithms into separate classes, all of which implement the same interface.