When to use Factory method pattern?

后端 未结 10 1474
南旧
南旧 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:37

    Although this isn't necessarily it's primary use, it's good for something where you have specialized instances of a class:

    public ITax BuildNewSalesTax()
    public ITax BuildNewValueAddedTax()
    

    You need both methods to build a tax object, but you don't want to have to depend on using "new" everytime because the constructors may be complex. This way I encapsulate all of the changes into a single method that is clear to others for future maintenance.

提交回复
热议问题