Factory Pattern to build many derived classes

前端 未结 3 434
北海茫月
北海茫月 2021-02-03 12:14

I have a factory object ChallengeManager to generate instances of a Challenge object for a game I\'m building. There are many challenges. The constru

3条回答
  •  眼角桃花
    2021-02-03 12:40

    You're "decentralizing" the factory, such that each subclass is responsible for creating itself.

    More commonly you would have a central factory that would know about the possible subtypes and how to construct them (often enough, simply by creating a new instance and returning that instance typed as a common interface or common base class). That approach avoids the issue you currently have. I also see no benefit to your current approach. You are currently gaining no encapsulation or code reuse over the more typical implementation of a factory.

    For additional reference, have a look at

    http://www.oodesign.com/factory-pattern.html

提交回复
热议问题