Summary

Strategies that occur in the real world may not naturally lie down as separate methods in a collection of classes. Logic that models alternative strategies may appear in a single class, often in a single method. Such methods may be too complicated and may mix strategy-selection logic with strategy execution.

To simplify such code, create a group of classes, one for each strategy. Define an operation and distribute it across these classes. This lets each class encapsulate one strategy, creating simpler code. You also need to arrange for the client that uses a strategy to be able to select one. This selection code may be complex even after refactoring, but you should be able to reduce this code so that it is nearly equivalent to pseudocode ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.