Design Patterns Java™ Workbook

(Michael S) #1
Chapter 23. Strategy

CHALLENGE 23.2


Fill in the class diagram in Figure 23.3, which shows the recommendation logic
refactored into a collection of strategy classes.

Figure 23.3. Complete this diagram to show a refactoring of the recommendation
software, with strategies appearing as implementations of a common interface.

Each implementation of the Advisor interface should supply a singleton that offers
the recommend() operation. You might correspondingly give each class a single, private
constructor to prevent other classes from instantiating it.


The constructor for PromotionAdvisor should investigate whether a promotion is on. You
might then supply this class with a hasItem() method that indicates whether there is
a promoted item.


package com.oozinoz.recommend;
import java.io.;
import java.util.
;
import com.oozinoz.fireworks.*;
public class PromotionAdvisor implements Advisor
{
public static final PromotionAdvisor singleton =
new PromotionAdvisor();
private Firework promoted;


private PromotionAdvisor()
{
try
{
Properties p = new Properties();
p.load(
ClassLoader.getSystemResourceAsStream(
"config/strategy.dat"));

Free download pdf