Creating a c# factory

.net, c#, design-patterns, factory

Solution

I think you are describing the factory pattern in the second part. The first part is not since it relies on the caller to know how to build the desired object. In your example, the `DateScheduleBuilderFactory` would be able to know how to interpret the information in the `request` object and return an object that derives from `DateScheduleBuilder`.

In short, like Johm Dom said above. You're already there...

Problem

So I'm not sure if this is a legit factory or not. Most factory's I see have something like this in the client: ``` if(//something) factory = new Type1Factory(); else factory = new RegularFactory(); ``` And then they create the object by going like `factory.Create();` So basically the condition to check for which factory you want is right in the calling code. I'd prefer to hide that and have the condition in the factory itself, which I guess wouldn't be called a factory anymore? Something like this: ``` DateScheduleRequest request = new DateScheduleRequest(); DateScheduleBuilder dateScheduleBuilder = new DateScheduleBuilderFactory(request).Create(); ``` And the `dateScheduleBuilder` object would basically be of a certain type depending on the request sent to the factory constructor. Is there another pattern for this or is this just a certain way to do factories? Basically, `DateScheduleBuilder` would be a parent class that a bunch of other types of builders inherit from, but my calling code knows that this abstract class has one method, and it doesn't need to be aware of the request type, just the fact that it needs to pass it to the factory and call one method.

Original source