Skip to content

Instantly share code, notes, and snippets.

@zcyemi
Created September 27, 2018 03:31
Show Gist options
  • Save zcyemi/21475d1945b1fbd22906606bb10d5e96 to your computer and use it in GitHub Desktop.
Save zcyemi/21475d1945b1fbd22906606bb10d5e96 to your computer and use it in GitHub Desktop.
Method chaining Pattern with generic derivation.C# and Typescript
public class A<T> where T :A<T>{
protected T t;
public T funcA(){
return t;
}
}
public class A:A<A>{
private A(){
this.t =this;
}
public static A create(){
return new A();
}
}
public class B<T>:A<T> where T :B<T>{
public T funcB(){
return t;
}
}
public class B:B<B>{
private B(){
this.t =this;
}
public static B create(){
return new B();
}
}
public class Program{
public static void test(){
var a = A.create().funcA().funcA().funcA();
var b = B.create().funcA().funcB().funcA();
}
}
class A<T extends A<T>>{
protected t:T;
public static ABuilder = class ABuilder extends A<ABuilder>{
private constructor(){
super();
this.t = this;
}
public static create():ABuilder{
return new ABuilder();
}
}
public funcA():T{
return this.t;
}
}
class B<T extends B<T>> extends A<T>{
protected t:T;
public static BBuilder = class BBuilder extends B<BBuilder>{
private constructor(){
super();
this.t = this;
}
public static create():BBuilder{
return new BBuilder();
}
}
public funcB():T{
return this.t;
}
}
let a = A.ABuilder.create().funcA().funcA().funcA();
let b = B.BBuilder.create().funcB().funcA().funcB();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment