From: Robert Klemme Date: 2009-03-26T19:53:48+09:00 Subject: Re: program design - template design pattern 2009/3/26 Srijayanth Sridhar : > While a lot depends on several other aspects of your project, this specific > problem depends on a principle question: > > Do you have any control over the feed object? I second that. Also, what type is "feed"? That's not clear to me. > If you have any control, then you could make a polymorphic superclass called > Feed and go on from there where each separate feed has its own related > methods to deal with its idiosyncrasies and polymorphic methods that deal > with common tasks. For ex: > > class Feed >   def initialize >   end >   def parse_content >   end One could also add # download the feed def fetch(uri) .... end > end > > class BbcFeed < Feed >   def parse_content >      # do something relevant here >      # probably return a string/hash/or content object >   end >   # define other methods specific to BbcFeed etc > end > > Once this is done, you just have to create the right object at runtime, > perhaps have a factory object detailing this. Or stuff the relationship between feed URI's and class that deals with it in a Hash: FEEDS = { 'http://foo.bar/feed.rdf' => BbcFeed, 'http://baz.boz.buz/path.rdf' => BbcFeed, 'http://what.ever.you/like' => OtherFeed, }.freeze Then you can do FEEDS.each |uri, cl| feed_handler = cl.new feed_handler.fetch feed_handler.parse_content end > If you don't have control, there isn't a way to run away from doing checks > on the object. Depending on how you determine the regex to use, you could > have specific solutions. Perhaps each feed has a different URL that you > could use to determine the parsing, or perhaps something else in the > xml...either way, you have to know what object you are dealing with, or you > have to write a parser that deals with it. Feed type checks could be done on the URI or the content. Depending on that a different approach needs to be followed but the basic pattern stays the same. Kind regards robert -- remember.guy do |as, often| as.you_can - without end