Skip to content

Instantly share code, notes, and snippets.

@stanroze
Created May 4, 2016 15:01
Show Gist options
  • Save stanroze/d66b62ed525cc626d0bc0198201f1f6b to your computer and use it in GitHub Desktop.
Save stanroze/d66b62ed525cc626d0bc0198201f1f6b to your computer and use it in GitHub Desktop.
Builder pattern
public class A {
public A() {
}
}
public class B : A {
public B(string param1){
}
}
public class C : B {
public C(string param1, string param2){
}
}
public static class Builder
{
public static A Create<T>(){
if(T is A){
return new BuilderA();
}
if(T is B){
return new BuilderB();
}
if(T is C){
return new BuilderC();
}
}
public class BuilderA{
public virtual A Build(){
return new A();
}
}
public class BuilderB{
protected string param1;
public BuilderB setParam1(string param1){
this.param1 = param1;
return this;
}
public A Build(){
return new B(this);
//or return new B(param1);
}
}
public class BuilderC{
protected string param1;
protected string param2;
public BuilderC setParam1(string param1){
this.param1 = param1;
return this;
}
public BuilderC setParam2(string param2){
this.param2 = param2;
return this;
}
public A Build(){
return new C(param1, param2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment