Created
March 8, 2013 17:57
-
-
Save SergeStinckwich/5118428 to your computer and use it in GitHub Desktop.
Fluent interface in Java
More information here: http://codemonkeyism.com/fluent-interface-and-reflection-for-object-building-in-java/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.*; | |
import java.beans.*; | |
public class FluentInterface<T> implements InvocationHandler { | |
Object obj; | |
public FluentInterface(Object obj) { | |
this.obj = obj; | |
} | |
public static <T> T create(Object object, Class fluentInterfaceClass) { | |
FluentInterface handler = new FluentInterface(object); | |
@SuppressWarnings("unchecked") | |
T fluentInterface = (T) Proxy.newProxyInstance( | |
fluentInterfaceClass.getClassLoader(), | |
new Class[]{fluentInterfaceClass}, | |
handler); | |
return fluentInterface; | |
} | |
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { | |
try { | |
String name = m.getName(); | |
if ("create".equals(name)) { | |
return obj; | |
} else { | |
String setter = "set" + name.substring(0, 1).toUpperCase() | |
+ name.substring(1); | |
Statement stmt = new Statement(this.obj, setter, args); | |
stmt.execute(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
} | |
return proxy; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private String firstName; | |
private String lastName; | |
private int age; | |
public static PersonFluentInterface with() { | |
return FluentInterface.create( | |
new Person(), PersonFluentInterface.class); | |
} | |
public Person() { | |
} | |
public void setAge(int age){ | |
this.age = age; | |
} | |
public void setFirstName(String firstName){ | |
this.firstName = firstName; | |
} | |
public void setLastName(String lastName){ | |
this.lastName = lastName; | |
} | |
public String toString(){ | |
return "Person("+firstName+", "+lastName+", "+age+")"; | |
} | |
public static void main(String[] args) { | |
Person john = Person.with() | |
.firstName("John") | |
.lastName("Doe") | |
.age(30) | |
.create(); | |
Person bob = Person.with() | |
.age(42) | |
.lastName("Black") | |
.firstName("Bob") | |
.create(); | |
System.out.println(john); | |
System.out.println(bob); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface PersonFluentInterface { | |
public PersonFluentInterface firstName(String firstName); | |
public PersonFluentInterface lastName(String lastName); | |
public PersonFluentInterface age(int age); | |
public Person create(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment