I have a situation, where i load a facebook feed,and depending upon the post type (video/image/link etc), i create corresponding classes. Each post type has a different corresponding model class and view class, however there is only one controller class which is instantiated for each post.
Now i need a way of dynamically deciding which model and view classes to create inside the controller for a particular post type. Right now, i have a creation helper class, which returns the corresponding model and view classes depending upon the post type. But it seems like a messy way of doing things. If have 20 something post types, thats 20 something if statements.
Is there a better way of doing this ?
You could keep a map (of sorts) of the post types against the classname used to handle the types:
And then instantiate the class instance using:
Or if you can guarantee that
postTypewill always be between 0 and 19, can just keep an array of classnames and use:You should ensure all handler classes derive from a common base class, to provide common functionality.