When to use Factory method pattern?

后端 未结 10 1469
南旧
南旧 2020-12-12 14:56

When to use Factory method pattern?

Please provide me some specific idea when to use it in project? and how it is a better way over new keyword?

10条回答
  •  佛祖请我去吃肉
    2020-12-12 15:41

    To answer the second part of you question from my opinion, I think the reason it's better than the 'new' keyword is that the factory method reduces the dependancy on constructors of particular classes. By using a factory method, you delegate the creation of the object in question to someone else, so the caller doesn't need teh knowledge of how to create the object.

提交回复
热议问题