Java Reference
In-Depth Information
private Advisor getAdvisor()
{
if (advisor == null)
{
if (PromotionAdvisor.singleton.hasItem())
{
advisor = PromotionAdvisor.singleton;
}
else if (isRegistered())
{
advisor = GroupAdvisor.singleton;
}
else if (isBigSpender())
{
advisor = ItemAdvisor.singleton;
}
else
{
advisor = RandomAdvisor.singleton;
}
}
return advisor;
}
private boolean isBigSpender()
{
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
return
spendingSince(cal.getTime()) > BIG_SPENDER_DOLLARS;
}
CHALLENGE 23.4
Write the code for Customer.getRecommended() .
CHALLENGE 23.5
In the new design, four singletons implement the Advisor interface in four ways,
encapsulating four strategies. Is the presence of multiple similar singletons
reminiscent of a design pattern?
Comparing Strategy and State
The refactored code consists almost entirely of simple methods in simple classes. This is an
advantage in its own right and makes adding new strategies easy. The refactoring relies
primarily on the idea of distributing an operation across a related group of classes. In this
regard, S TRATEGY is identical to S TATE . In fact, many developers wonder whether these are
really different patterns.
On the one hand, in the real world, strategies (such as recommending a firework) and states
(such as a carousel door with a one-touch control button) are clearly different ideas. This real
Search WWH ::




Custom Search