Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created July 7, 2011 19:44
Show Gist options
  • Save jarrodhroberson/1070367 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/1070367 to your computer and use it in GitHub Desktop.
Fluent API Enforcement in Java using Interfaces
import java.util.Date;
public class PermissionManager
{
public static AssignPermission grantUser(final String user)
{
return new Rights(user);
}
private static class Rights implements AssignItem, AssignPermission, SetDate
{
private String user;
private String permission;
private String item;
private Date date;
private Rights(final String user) { this.user = user; }
public SetDate item(final String i) { this.item = i; return this; }
public AssignItem permissionTo(final String p) { this.permission = p; return this; }
public void asOf(final Date d) { this.date = d; /* do persistence or other actions here */ }
}
public interface AssignPermission { public AssignItem permissionTo( String p ); }
public interface AssignItem { public SetDate item( String i ); }
public interface SetDate { public void asOf( Date d ); }
public static void main(final String[] args)
{
PermissionManager.grantUser("me").permissionTo("ALL").item("EVERYTHING").asOf(new Date());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment