Motivation for Simple Factory and Factory Method Pattern

后端 未结 6 901
猫巷女王i
猫巷女王i 2020-12-16 20:22

I know there are a lot of questions out there about differences of different factory patterns, but the answers are so different and confusing. The books that i read use uncl

6条回答
  •  一个人的身影
    2020-12-16 20:45

    Here is a simple guideline I can think of following -

    The basic differentiation between these two is one provides a concrete creator (Simple Factory) while the other(Factory method) supports decoupling of creator by inheritence so that multiple creators can be supported. An example to make it clearer -

    1. Factory Method - Consider creating a Button with different types - Toggle, ImageButton etc. Its more likely that you would have different creators of Button. For eg. based on different operating systems, WindowsButtonFactory, OSXButtonFactory etc. Factory method is suitable in this case with an abstract class ButtonFactory being implemented by WindowsButtonFactory, OSXButtonFactory (and many more in future).
    2. Simple Factory - Consider creating Book based on genre - Fiction, NonFiction etc. More than often you would think a concrete Factory should be enough here.

提交回复
热议问题