It's not a secret anymore!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Any design patter for this ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply locked New topic
Author

Any design patter for this ??

Jason Hankins
Greenhorn

Joined: Oct 17, 2003
Posts: 3
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 ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Just noticed that this is crossposted in the Java in General (intermediate) forum: http://www.coderanch.com/t/372010/java/java/Whats-best-way
Please continue discussion there. I will close this thread.
Please don't crosspost in the future. Thanks.
 
Don't get me started about those stupid light bulbs.
 
subject: Any design patter for this ??
 
Similar Threads
textbox to listbox(simple)
Forms in servlet
session tracking remove item (urgent)
selecting between two list boxes
Whats the best way ??