Views: 8.9K
Replies: 1
Archived
|
What Design Pattern to use for collections of parsersI have string and I need to pass it through different parsers and eventually get the processed string.
Lets say, I have string s = "<input type='text' /> some text <span>hello</span> some text <select></select>";Now, 1st parser will handle all "input" elements, 2nd parser will handle all "span" elements, and etc. Each parser will do some work with the string and eventually the processed string will be returned. Could anyone offer advise as to which Design Pattern I should use and/or how to do this in an 'elagant' way? Thank you Igor M, Jan 29, 2011
I believe a 'strategy' pattern would fit this nicely. Basically, a strategy is a collection of algorithms. You'd have an abstract base class called 'Parser'. Then you'd have derived or "concrete" strategy classes the implement the specialized parsing (for each element in your case). The idea here is that the concrete strategies can share common code with the base class and use polymorphism to address each parsing nuance with each tag. Use the strategy example on this site to get more details.
Feb 01, 2011
|
|
Reply 1This seems like a use for the chain of responsibility. Pass it to the first parser, and it will parse the parts it can and pass the rest to the second parser, and so forth.
Stephanie Schwinn, Feb 17, 2011
|