Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created August 8, 2012 22:32
Show Gist options
  • Save egonelbre/3299448 to your computer and use it in GitHub Desktop.
Save egonelbre/3299448 to your computer and use it in GitHub Desktop.
Foo java
// V1
public class Foo {
}
public class KungFoo extends Foo {
public KungFoo(string owner){
// ...
}
}
public class FooBarred extends Foo {
public FooBarred(string owner){
// ...
}
}
public class FooKing extends Foo {
public FooKing(string owner){
// ...
}
}
public Foo createFoo(Class fooClass, string owner = "Bar"){
Foo myFoo = new fooClass(owner);
// some additional handling
return myFoo;
}
// V2
// can be implemented with method passing in Java8
public abstract class FooSpec {
public void init(Foo f);
}
public class KungFoo extends FooSpec {
public void init(Foo f){
// ...
}
}
public class FooBarred extends FooSpec {
public void init(Foo f){
// ...
}
}
public class FooKing extends FooSpec {
public void init(Foo f){
// ...
}
}
public Foo createFoo(FooSpec fooSpec, string owner = "Bar"){
Foo myFoo = new Foo(owner);
fooSpec.init(myFoo);
// some additional handling
return myFoo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment