If you are using .NET you could build it with reflection instead. For example, if you were creating a plugin system then you would have a folder to drop plugin DLLs into. Then your factory would look at the available DLLs, examine each one for the appropriate reflection attributes, then match those attributes against whatever string was passed in to decide which object to select and invoke.
This keeps you from having to recompile your main app, though you'll have to build your workers in other DLLs and then have a way to tell your factory which one to use.
Here's some really fast and dirty pseudo code to get the point across:
Assuming you have a DLL assembly called Workers.DLL
Set up an attribute called WorkerTypeAttribute with a string property called Name, and the constructor to be able to set that Name property.
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class WorkerTypeAttribute : Attribute
{
string _name;
public string Name { get { return _name; } }
public WorkerTypeAttribute(string Name)
{
_name = Name;
}
}
You'd then apply this attribute to any worker class that you've defined like:
[WorkerType("CogWorker")]
public class CogWorker : WorkerBase {}
Then in your app's worker factory you'd write code like:
public void WorkerFactory(string WorkerType)
{
Assembly workers = Assembly.LoadFile("Workers.dll");
foreach (Type wt in workers.GetTypes())
{
WorkerTypeAttribute[] was = (WorkerTypeAttribute[])wt.GetCustomAttributes(typeof(WorkerTypeAttribute), true);
if (was.Count() == 1)
{
if (was[0].Name == WorkerType)
{
// Invoke the worker and do whatever to it here.
}
}
}
}
I'm sure there are other examples of how to do this out there, but if you need some more pointers, let me know. The key is that all of your workers need to have a common parent or interface so that you can invoke them the same way. (I.e. all of your workers need a common "Execute" method or something that can be called from the factory, or wherever you use the object.