IHandler first = HandlerFactory.CreateInstance(
ParamBuilder.start().withOne("x").withTwo("y").build());
IHandler second = HandlerFactory.CreateInstance(
ParamBuilder.start().withAlpha(1).withBeta(new DateTime()).build());
IHandler third = HandlerFactory.CreateInstance(
ParamBuilder.start().withMickey(":)").withMouse(1L).build());
public static IHandler CreateInstance(HandlerType type, IParam param)
case HandlerType.Handler1: return new Handler1(param);
case HandlerType.Handler2: return new Handler2(param);
case HandlerType.Handler3: return new Handler3(param);
default: throw new NotSupportedException();
enum HandlerType { Handler1, Handler2, Handler3
interface IHandler { void DoStuff(); }
class Handler1 : IHandler {
private Param1 numbers; private Param2 letters; private Param3 disney;
public Handler1(IParam param) {
if (param is Param1) numbers = (Param1) param;
if (param is Param2) letters = (Param2) param;
if (param is Param3) disney = (Param3) param;
public void DoStuff() { Console.WriteLine("I am alive - Handler 1");}
class Handler2 : IHandler {
private Param1 numbers; private Param2 letters; private Param3 disney;
public Handler2(IParam param) {
if (param is Param1) numbers = (Param1) param;
if (param is Param2) letters = (Param2) param;
if (param is Param3) disney = (Param3) param;
public void DoStuff() { Console.WriteLine("I am alive - Handler 2");}
class Handler3 : IHandler {
public Handler3(IParam param) {
if (param is Param1) numbers = (Param1) param;
if (param is Param2) letters = (Param2) param;
if (param is Param3) disney = (Param3) param;
public void DoStuff() { Console.WriteLine("I am alive - Handler 3");}
string One { get; } string Two { get; }
int Alpha { get; } DateTime Beta { get; }
string Mickey { get; } long Mouse { get; }
private int? alpha; private DateTime? beta;
private string mickey; private long? mouse;
public static ParamBuilder start() { return new ParamBuilder(); }
if (!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(one)) {
return new Param1(one, two);
if (alpha.HasValue && beta.HasValue) {
return new Param2(alpha.Value, beta.Value);
if (!string.IsNullOrEmpty(mickey) && mouse.HasValue) {
return new Param3(mickey, mouse.Value);
throw new NotSupportedException();
public ParamBuilder withOne(string one) { this.one = one; return this; }
public ParamBuilder withTwo(string two) { this.two = two; return this; }
public ParamBuilder withAlpha(int alpha) { this.alpha = alpha; return this; }
public ParamBuilder withBeta(DateTime beta) { this.beta = beta; return this; }
public ParamBuilder withMickey(string m) { this.mickey = m; return this; }
public ParamBuilder withMouse(long m) { this.mouse = m; return this; }
public string One { private set; get; }
public string Two { private set; get; }
public int Alpha { get {throw new NotImplementedException();} }
public DateTime Beta { get {throw new NotImplementedException();} }
public string Mickey { get {throw new NotImplementedException();} }
public long Mouse { get {throw new NotImplementedException();} }
public Param1(string one, string two) {
public int Alpha { private set; get; }
public DateTime Beta { private set; get; }
public string One { get {throw new NotImplementedException();} }
public string Two { get {throw new NotImplementedException();} }
public string Mickey { get {throw new NotImplementedException();} }
public long Mouse { get {throw new NotImplementedException();} }
public Param2(int alpha, DateTime beta) {
Alpha = alpha; Beta = beta;
public string Mickey { private set; get; }
public long Mouse { private set; get; }
public string One { get {throw new NotImplementedException();} }
public string Two { get {throw new NotImplementedException();} }
public int Alpha { get {throw new NotImplementedException();} }
public DateTime Beta { get {throw new NotImplementedException();} }
public Param3(string mi, long mo) {