Re: Case expression must be constant expression
Ed Kirwan wrote:
Daniel Pitts wrote:
In Object Oriented design, its considered a likely problem if you have a
switch statement (or a switch like construct). While there are *some*
times when a switch is appropriate, I've found that I haven't used a
switch statement once I understood the State, Strategy, and Flyweight
patterns.
Hej, Daniel,
Would you say that a switch statement is inappropriate in a parameterised
factory method, such as the one below?
http://www.edmundkirwan.com/servlet/fractal/cs1/code/package101.html#getEvaluation
And if inappropriate, how should parameterised factory methods look? Or
would you say that parameterised factory methods are inherently un-OO?
I'm curious because I use them quite a lot and am wondering whether I should
drop them in favour of something else.
In your example, where does rank come from?
Is it assigned specific values based on another if/else block?
This seems to me to fit my example of using an enum as a flyweight:
<http://virtualinfinity.net/wordpress/program-design/2007/10/22/using-enums-as-a-flyweight-pattern/>
Basically, you have a Rank abstract class/interface which has the
implementations HighestCard, OnePair, TwoPairs, etc....
The interface would have a HandEvaluation getEvaluation(Hand hand);
Your factory method could remain. It would simply delegate to
rank.getEvaluation(hand);
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>