The answer to the old question.
I took a look at Apache Zest. It may have been just me, but I find the examples a bit cumbersome. And I could not understand. Another alternative could be object commands.
But I suggest you take a look at this repo:
https://github.com/Mashashi/javaroles/
This may partially cover what you want to do. Seems simple.
Here is an example:
Defining an interface for roles:
public interface Human { String hello(); String die(String age); String eat(); String dance(); } public interface Monkey {String hello(); String eat();}
Defining a Hard Type AnimalRoles ...
public class AnimalRoles implements Human, Monkey{ public static final String HALLO = "Default hallo"; public static final String DIE = "Default they kill me..."; public static final String EAT = "Default eat..."; @ObjectForRole public Human human; @ObjectForRole public Monkey monkey; public AnimalRoles(Human human, Monkey monkey){ this.human = human; this.monkey = monkey; if(this.human!=null){ ((Portuguese)this.human).core = this; } } @Override public String hello() { return HALLO; } @Override public String die(String age) { return DIE+age; } @Override @TurnOffRole public String eat() { return EAT; } @Override public String dance() { return "Just dance"; } public String notInRole(){ return "Oh oh"; } }
Defining the role of the Bonobo class ...
public class Bonobo implements Monkey{ public Bonobo() {} @Override public String hello(){ return "Ugauga"; } @Override public String eat() { return "Nhamnham"; } }
Defining the role of the Portuguese class ...
@RoleObject(types = { AnimalRoles.class }) public class Portuguese implements Human{ public static final String HALLO = "Hey there"; public static final String DIE = "They killed me"; public static final String EAT = "Eating boiled pork now"; public AnimalRoles core; public Portuguese() {} @Override public String hello() { return HALLO; } @Override public String die(String age) { return DIE+age; } @Override public String eat() { return EAT; } @Override public String dance() { return core.dance()+" modified!"; } }
Running test ...
new RoleRegisterComposition().registerRools(); AnimalRoles a = new AnimalRoles(new Portuguese(), new Bonobo()); System.out.println(a.hello()); System.out.println(a.dance());
There will be a seal ...
"Hey there" "Dance modified!"
skynetDude Feb 09 '16 at 18:36 2016-02-09 18:36
source share