Factory Methods vs. Overloaded Constructors

Factory Methods vs. Overloaded Constructors

Old forum URL: forums.lhotka.net/forums/t/2569.aspx


gderoeck posted on Wednesday, March 21, 2007

Hello!

Please forgive my ignorance on this subject, but recently I was asked why factory methods are used in the CSLA business objects instead of just overloading constructors.  I searched through the book and found a couple reasons.  One is to not expose the constructor directly.  The other is to give the opportunity to check authorization of the action being called.

Am I off on these two?  Can anyone give me explanations as to why these are important or offer other reasons?  I person that asked me this question said that the authorization can be done within the constructor as well.  I don't know enough about OOP or CSLA to offer intelligent arguments for these, so any help would be appreciated.  Thank you!

-G

SonOfPirate replied on Wednesday, March 21, 2007

Another key reason is the use of the DataPortal architecture to allow for deployment in distributed environments.  In order to implement these features, you have to call a factory method which can initiate the data portal mechanism and return the BO object to the calling code.  By instantiating a class with the constructor you are dealing directly with an instance of that class and cannot perform the same operations.

HTH

 

gderoeck replied on Wednesday, March 21, 2007

Thank you for the reply!

In general, does using factory methods increase the complexity of the class?  In my opinion, it makes writing the UIs easier and if the factory methods are always used, obviously there's consistency across the classes.

Are there any arguments AGAINST using factory methods?  Thanks again.

-G

Copyright (c) Marimer LLC