Factory method pattern: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 45:
1 - A bad way to design the ImageReader
 
This [[method]] has the [[advantage]] of abstracting the [[file type]] from the [[class]] that calls this ImageReader, but as the number of file types supported gets larger, the code will quickly become huge, unwieldy and hard to maintain.
 
Another [[solution]] that seems possible is to have a separate [[object]] for each of these file types:
Line 96:
Again, there are [[advantage]]s to this [[method]] (clean [[organisation]] of the ImageReader classes, split up in several classes), but this is at the [[cost]] of the [[abstraction]] of the image type. Again, when you get to have dozens of file types, this will be unsatisfactory and produce code that is hard to maintain.
 
So what's the [[solution]]? Simply to take the best of both worlds, by using the Factory pattern (which you should already have an idea of by now). The [[Factory]] is a [[class]] that returns another class depending on the [[context]]. So in this [[situation]], keeping the separate [[class design]] as in the last example:
 
public class ImageReaderFactory