Reputation: 11
I have a question about the usage of "utility/helper" methods in a factory class. Consider an example of an XML string that reprents a document. I have a class that transforms it to an "object" (say PDF, Word, CSV, etc.). I have a factory class (lets call it DocumentFactory) that accepts this XML string and based on certain rules gives back the correct document object.
My question here is that in terms of "best practices" is it ok for me to add "utility/helper" methods to the DocumentFactory class that aid in deciding that type of object will be returned? These helpers are beyond simple if/swtich case statements. But not more than 15-20 lines.
I am using one private static class as well in my code and there are about 4-5 helper methods (the helpers are public since I have tests written for these).
So is this setup a valid one for a factory class?
Upvotes: 1
Views: 1613
Reputation: 1044
This is perfectly fine.. In fact, I'd say that using helper methods is the preferred way of doing it, since it is good practice to chunk up your code into as many re-usable methods as possible. You should probably make these helper methods private (and static, assuming the factory method itself is static).
Upvotes: 0
Reputation: 9705
No, there is nothing intrinsically wrong with using helper methods in a Factory to help decide what sort of object to return. All the usual method-related warnings apply, but there are no Factory-specific reasons to avoid them.
Upvotes: 1