This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Is there any design pattern that can be used in a scenario, where a class has to handle String manipulations. The requirement is that for every type of html field, I have to insert a default value for it. The default value will be retrieved form a map based on the parameter. I will have a class StringWorker which will have a method, workOnString(String main, Map map, String type). The StringWorker class has to first find out the key in the main string, and the get the value from the Map based on the key, and then replace the key with the value. For eg lets say the main string looks like this "<input type=�text� name=�first� constant=�key�>", so in the StringWorker class I will parse the main string to retrieve �constant=key�, then I will retrieve the �key�, and then based on this key I will retrieve the value from the map, and then I will replace the �constant=key� with the value=valuefrommap in the main String. So finally the main string will look like this "<input type=�text� name=�first� value=�valuefromMap�>" Second scenario which is bit complex is the handling of the string in cases of list, for eg in case of list, the main string will look like this "<select name=�menu�><option value=�item1� constant=�key�><option value=�item2� constant=�key� ><option value=�item3� constant=�key�>". In this case I have to replace constant=�key� with selected attribute but I don�t know which option is selected, so based on the key I will get the value from the Map, and the compare the value returned from map with the value in the option tag, and wherever value matches, I will replace the constant=�key� with selected, and leave other option tag as it is. For eg if the selected option is item 2, then the after all the manipulations, the string will look like this "<select name=�menu�><option value=�item1� constant=�key�><option value=�item2� selected ><option value=�item3� constant=�key�>" Is there any better way to handle this sort of requirement. Is there any java design pattern that can be used for such sort of implementation. Regards escaped < and > so that the post can be read in a browser [ October 17, 2003: Message edited by: Frank Carver ]
Design patterns generally introduce abstraction/indirection to solve a structural problem. Yours doesn't qualify, in my opinion, so there probably isn't a "design pattern" for your situation. I might be wrong, of course.
I would probably use regular expressions for the parsing of the strings - not a design pattern, but might help nevertheless. But I wonder: where are the original strings coming from?
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