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.
1. Needs to loop endlessly, at the beginning of each loop ask for player input.
2. Needs to invoke methods based on that player input.
3. Needs to recognize commands even if player only partially types out the word. For example, 'e' instead of 'east.'
So my thought is that I need to create a collection of strings which are linked to method invocations. I feel that the collection part is important, because then I can accomplish # 3 with: if(string.contains("e")). Additionally, I could have a player command which prints the collection, thus informing a player of all potential commands.
The most efficient way of doing this that I can think of is just initializing all the strings at runtime, then adding them to an ArrayList<String>, then doing a long if-else-if statement on the contents of the ArrayList<String> vs. the player command.
But it seems like there should be a better way... For example, is there any way to manuever HashMap into storing method invocations as values? That way I 'key' strings and then map corresponding methods as values. That would cut the if-else-if statement down a TON, and it would be far more versatile in adding new commands.
- SCJA 1.0
Joined: May 22, 2008
My apologies, I should have just tried to do it. It's entirely possible, with no tricks whatsoever. Very cool.
In java, instead of thinking method invocations, I think callback routines/functions which simplifies the understanding for me
You could possibly do the following
It's essentially a method invocation, but not really
HTH Ashish Hareet
Joined: May 22, 2008
Thanks a lot Ashish! I'll sit down and test this out a little bit later, but this sounds like a good place to start.
As a side note, I definitely haven't rounded out my interface/inheritance/polymorphism toolkit to the extent that I could/should. After learning that you can just handle variability amongst instances of a class with a HashMap, and just add whatever uniqueness to a class you want by introducing new values, it seems like this would be the natural tendency.
I guess I still need to round out that other toolkit though. =)
By the way, Ashish's solution makes use of the Command design pattern.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus