Abstract Factory With Reflection
In Chapter 12, page 375, the Author mentions an option in which one could instantiate a factory by means of reflection.
In my opinion this is preferable, as it adheres more closely to the Open-Closed principle. This way, the DataProviderFactory remains open for extension, but closed for modification.
In case anyone is interested on how that works, I have attached a solution (.ZIP file) which shows the implementation.
About the example, please note:
1. A default implementation is provided, so that OleDbProviderFactory is used if no provider is specified.
2. The DataProviderFactory class is implemented as a singleton. A static "Instance" property is used to ensure that no more than one provider instance can ever exist.
3. To enforce the singleton behavior, the three concrete providers have protected constructors in place, so that they cannot be instantiated in any other way except through the Instance property mentioned above. In other words, it prevents you from doing this:
DataProviderFactory factory = new MsSqlProviderFactory();
Enjoy.
Last edited by Lee Dumond; October 1st, 2009 at 04:00 PM..
Reason: edited: upload a revised sample
|