How does a factory know which type of object to create?

前端 未结 5 1716
旧时难觅i
旧时难觅i 2021-02-05 12:04

I believe the factory method design pattern is appropriate for what I\'m trying to do, but I\'m not sure how much responsibility (knowledge of subclasses it creates) to give it.

5条回答
  •  既然无缘
    2021-02-05 12:28

    For extensibility, you can externalize some of these dependencies you mention. Like figuring out what kind of file it is or mapping the file type to a class that handles it. An external registry (i.e., properties file) would store say, GIF -> GifReader, or better GIF -> GifMetadataClass. Then your code could be generic and not have dependencies on all the classes, plus you could extend it in the future, or 3rd parties could extend it.

提交回复
热议问题