Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Factory vs Factory method

 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have question related to chapter 4 (Baking with OO goodness) - the factory pattern.

My question is what are the advantages offered by the Factory method pattern over the Simple Factory programming idiom? Why do we need Factory method when we have the Simple Factory idiom?

My argument is that the Simple Factory idiom is based on composition and delegation while the Factory method is based on inheritance. After reading the first 3 chapters, it is obvious that we should favor composition over inheritance due its advantages (dynamic behavior changes and flexibility). This being the case why need to use the Factory method pattern? What is it that you can do with the Factory method that you cannot do with the Simple Factory more flexibly and dynamically?

The book says that the Factory method pattern localizes the factory (the factory method) and the client (calling method) to the same class and hence the same framework and you can enforce tighter control on the client by making the client method final. These arguments are somehow not that appealing to me. Also with the Simple Factory, you can compose a Factory interface and delegate the call at run-time to any concrete factory class implementing that interface. This is more dynamic and flexible than having the factory implementation in a sub-class

Would like to know your views on this.
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to the Head First Design patterns book. If you are not a reader of that book, its ok, just ignore the first line in my post and start reading from the second
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic