Factories or Providers: what is your chosen terminology?

The Factory pattern in plain language is about encapsulating an entity that is responsible for creating stuff provided with a given context. For example, say I wanted to create such an entity responsible for created different implementations of an ILoggerFacade interface defined as such:


public interface ILoggerFacade
{
void Error(object error);
void Info(object info);
}

I could define an ILoggerFacadeFactory interface as follows:


public interface ILoggerFacadeFactory
{
ILoggerFacade CreateLogger(object owner);
}

and have an implementation for Console, log4Net or other logging libraries as follows:

public class ConsoleLoggerFacadeFactory : ILoggerFacadeFactory
{
public ILoggerFacade CreateLogger(object owner)
{
return new ConsoleLoggerFacade(owner);
}
}


public class Log4NetLoggerFacadeFactory : ILoggerFacadeFactory
{
public ILoggerFacade CreateLogger(object owner)
{
return new Log4NetLoggerFacade(owner);
}
}

Then this becomes a matter of registering the appropriate factory in an IoC container, such as Unity, to create the logger as desired:

   ioc.Register<ILoggerFacadeFactory, Log4NetLoggerFacadeFactory>();

This way, I can easily change the type of logger I use in my application via configuration. I use this pattern extensively, especially with 3rd party libraries. I have used this to abstract message queue implementations for MSMQ, RabbitMQ and ZeroMQ, logging and even for UI elements such as DataGrids, DataCharts and Docking containers, without thinking much about the nomenclature.

Recently, however, this terminology has caused some excitement in my professional circles. Others will rather call these types of entity factories, entity providers for good cause. After all, a provider provides stuff. Factories or Providers sort of provide similar responsibilities even though I would argue that factories create stuff while providers do not necessary do so. Either way, there is no right or wrong answer here although I tend to lean toward calling entity creators, factories, for the very reason that the create stuff by typically calling some sort of constructor on the object class.

Just like an automobile factory manufactures cars, an entity factory instantiates new objects. An automotive dealer, on the otherhand, is an auto provider but cannot be considered an automobile factory. This is the way I distinguish these responsibilities. In fact, a provider can be given reference to a factory that will create stuff that is requested of it, it not in its inventory. Distinct responsibilities.

This is the way I see it. What are your thoughts?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s