Skip to main content
Removing unnecessary type casting
Source Link
public class Example { public void ExampleMethod() { Handler1Factory factory1 = new Handler1Factory(); Handler1var handler1 = (Handler1)HandlerFactory.Create(factory1, "parameter1", "parameter2"); Handler2Factory factory2 = new Handler2Factory(); Handler2var handler2 = (Handler2)HandlerFactory.Create(factory2, 1, 2); } } 
public class Example { public void ExampleMethod() { Handler1Factory factory1 = new Handler1Factory(); Handler1 handler1 = (Handler1)HandlerFactory.Create(factory1, "parameter1", "parameter2"); Handler2Factory factory2 = new Handler2Factory(); Handler2 handler2 = (Handler2)HandlerFactory.Create(factory2, 1, 2); } } 
public class Example { public void ExampleMethod() { Handler1Factory factory1 = new Handler1Factory(); var handler1 = HandlerFactory.Create(factory1, "parameter1", "parameter2"); Handler2Factory factory2 = new Handler2Factory(); var handler2 = HandlerFactory.Create(factory2, 1, 2); } } 
Source Link

The answer provided by @JohnWu is very elegant and I like it a lot. Allow me to provide an alternative.

You could use Abstract Factory Pattern. You would basically have one big factory of factories, which would then produce the concrete factories for concrete handlers. Then, should you need another handler, all you would need to do is write one factory for that particular handler. It would be something along these lines:

public interface IHandler { } public class Handler1 : IHandler { public Handler1(string param1, string param2) { } public Handler1(int param1, int param2) { } } public class Handler2 : IHandler { public Handler2(string param1, string param2) { } public Handler2(int param1, int param2) { } } 

For simplicity, I have only two Handler classes with two different constructors. I believe the solution still can be applied to your case. Next, we have factories:

 public interface IHandlerFactory { IHandler Create(string param1, string param2); IHandler Create(int param1, int param2); } public class Handler1Factory : IHandlerFactory { public IHandler Create(int param1, int param2) { return new Handler1(param1, param2); } public IHandler Create(string param1, string param2) { return new Handler1(param1, param2); } } public class Handler2Factory : IHandlerFactory { public IHandler Create(int param1, int param2) { return new Handler2(param1, param2); } public IHandler Create(string param1, string param2) { return new Handler2(param1, param2); } } public class HandlerFactory { public static IHandler Create(IHandlerFactory factory, string param1, string param2) { return factory.Create(param1, param2); } public static IHandler Create(IHandlerFactory factory, int param1, int param2) { return factory.Create(param1, param2); } } 

The alternative here would be to have a HandlerFactory class that would have a property of IHandlerFactory type, and the methods would not be static and would not have IHandlerFactory parameter in them. Obviously, an object of HandlerFactory class would need to be instantiated at some point.

The usage would go somehow like this:

public class Example { public void ExampleMethod() { Handler1Factory factory1 = new Handler1Factory(); Handler1 handler1 = (Handler1)HandlerFactory.Create(factory1, "parameter1", "parameter2"); Handler2Factory factory2 = new Handler2Factory(); Handler2 handler2 = (Handler2)HandlerFactory.Create(factory2, 1, 2); } } 

The solution clearly divides the code by purpose, and most importantly, should you have the need for another handler, you would not need to change the existing code. All you would have to do is write a factory for that particular handler.

Remember, this is only possible because all handlers have the constructors with same parameter lists. The design would be somewhat different if that was not the case.