DEV Community

Discussion on: How to avoid the Factory pattern in C#

Collapse
 
thompcd profile image
Corey Thompson

I don't know that this accomplishes the main issue, you still have to know the concrete class in advance. You don't have a concrete class but still have to recompile to add a new type to your enum. I feel that I shouldn't need to add all concrete types to my factory class. By using the author's delegate method, I can define all of my concrete classes where they best fit in my project and just implement a good interface that indicates all of the uses of that class.

The downside of this is definitely traceability though.. Getting a new person on the team to be able to find the source and see what's happening at a glance is a nice perk for sure, which doesn't happen in the more abstract version.

I started doing a lot of JS a year ago.. It seems it's tainted me.

Collapse
 
thatblairguy profile image
That Blair Guy

The enum certainly forces a recompile, but you can use strings instead.

But the Creator methods (whether declared as delegates or Funcs) would encapsulate the knowledge of the concrete classes. All the factory needs to know is that it has a function to call and that function has a specific signature.