This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Or if this is a real-life question and not just a beginner's programming exercise, you could replace the whole thing by calls to various methods in the TimeZone class. No "ifs" would be required there.
Joined: Jan 17, 2006
You dont have to use if's if you use enums. You could use the valueOf() method.
Joined: Feb 03, 2006
Well I am in intermidate programming but its the beginner stuff I don't understand.....I am having trouble with the basic concepts...thats why I post here
Sounds like the perfect fit for the Strategy Design Pattern. Some proponents of language theory proclaim that a single if/else is a broken construct and does not belong in a complete language. While I haven't bought that argument just yet, I certainly agree with the more wider (though still small relative to the critical mass) understanding that a if/else/else (3 or more statement bodies) should always be avoided. I even know a few who claim that for loops are flawed constructs!! I haven't bought that argument, simply because I don't understand it in completeness yet, but I suspect that they are ultimately right. For the same reason - appropriate abstraction/accurate requirement expression - switch/case constructs should always be avoided.
The point being, I suspect that your teacher (a knowledagable academic?) is probably trying to teach you something related to this and I encourage you to pursue it further.
The Strategy Design Pattern - one of few legitimate design patterns - is implemented in Java almost always using a java.util.Map. If you wish to go for further purity (but falling infinitely short implied by Java), you might want to consider net.tmorris.adt.map.Map - or just write the interface yourself. Minimalism is paramount even though it is often unachievable under a given context. So often I observe suffering as a result of excessive expression of requirement/contract, and worse, I observe the resulting "problem" attributed to a consequence, instead of the underlying flaws.
Good to hear of a tertiary institution on the right path ...if indeed that is what is happening.