views:

150

answers:

3

Is it wrong to call a class a FooFactory if it doesn't always create Foo objects? For example if I have the following interface:

public interface IFooFactory
{
    Foo Create();
}

and implement it as follows:

public class FooFactory : IFooFactory
{
    public IFoo Create()
    {
        return ServiceLocator.Current.GetInstance<IFoo>();
    }
}

then this class might create a Foo depending upon how my IoC container is configured. If the 'XxxFacotry' name should be reserved for true factories, what should I call my interface and class?

The obvious answer is IFooProvider, but I really want avoid 'XxxProvider' because it's overused and therefore too vague. On the other hand, IFooServiceLocator is far too specific.

Alternative naming suggestions greatly appreciated.

+3  A: 

Your class provides an IFilter, which may or may not entail creating one. I fail to see how FilterProvider is too vague.

If you don't like it, what about FilterSource?

Honestly, though, I don't think it'd be so bad to call it FilterFactory; I mean, who cares that much how it's implemented? If someone calls your Create method, chances are they're caching the result, which makes it pretty moot whether or not they know that Create doesn't actually instantiate a new object from scratch.

Either way, the right thing to do is document your class's actual functionality, including whatever details will be relevant to those utilizing it (and omitting those details which are, in fact, irrelevant or unimportant).

Dan Tao
@Dan Tao I'm not sure that documenting the concrete class would help as consumers will be using the interface for accessing the factory. Therefore, they must rely on the contract implied by the name and not documentation of the concrete class. I concede though, that IFilterProvider may be acceptable, and that my objection to it is purely subjective.
Damian Powell
@Damian: Picky picky! You're right; I should have said "type's" rather than "class's"; my point was that you should document whatever needs to be documented. In the case of `IFilterFactory`, for example, regardless whether or not others interpret the -`Factory` part to mean that an implementation creates a new instance, you could put into the documentation: "An `IFilterFactory` provides an instance of an `IFilter` implementation via the `Create` method. This is not guaranteed to create a new object." But then with that definition it really seems like `IFilterProvider` might make more sense.
Dan Tao
+3  A: 

Would you call something a cookie factory if it sometimes made cake?

Proclyon
Depends upon [whether VAT is chargeable](http://en.wikipedia.org/wiki/Jaffa_cakes#Cake_or_biscuit.3F).
Paul Ruane
Or better: Would you call some place a cookie factory if you showed up and they only had a few cookies and they were like, "Share, everybody!"
Dan Tao
Succinctly put!
Damian Powell
@Dan, I think I'd be suspicious and call it a mafia front.
Dr. Wily's Apprentice
A: 

So far, it seems to be 50/50. So the answer I'm accepting is the one that implies 'Yes; it is wrong to name something as a FooFactory if it doesn't always create new Foo objects' because that's where my own preference lies.

Damian Powell
I feel you made a wise decision there
Proclyon