Refactoring to Strategy
The getRecommended() method presents several problems. First, it's long—long enough that comments have to explain its various parts. Short methods are easy to understand, seldom need explanation, and are usually preferable to long methods. In addition, the getRecommended() method chooses a strategy and then executes it; these are two different and separable functions. You can clean up this code by applying STRATEGY. To do so, you need to
Create an interface that defines the strategic operation
Implement the interface with classes that represent each strategy
Refactor the code to select and to use an instance of the right strategic class
Suppose that you create an Advisor interface and begin your refactoring by introducing ...
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.