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

前端 未结 5 1789
旧时难觅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:22

    I would have a static CanReadFrom method (or something) in the common ImageReader interface (not sure if this is possible -- FIXME). Use reflection to grab all implementors and call the function. If one returns true, return an instance of the class.

提交回复
热议问题