Hi.
I'm currently working on a demo for a talk where I'm using Qi4j to
illustrate how one can do protoype based development with Java. For
reference, I'm including a Ruby example of this taken from the talk;
# Clone an instance of a person
nancy_cartwright = a_person.clone
nancy_cartwright.first_name = "Nancy"
nancy_cartwright.name = "Cartwright"
# Create another person with different to string behavior
hayashibara_megumi = a_person.clone
hayashibara_megumi.first_name = "Megumi"
hayashibara_megumi.name = "Hayashibara"
# Redefine the to string method
def hayashibara_megumi.to_s
"#{name} #{first_name}"
end
# Create yet another person with "special needs"
inoue_kikuko = a_person.clone
inoue_kikuko.first_name = "Kikuko"
inoue_kikuko.name = "Inoue"
# Copy the redefined method from the previous person and bind it to
this instance.
japanese_to_s = hayashibara_megumi.unbind_method(:to_s)
inoue_kikuko.bind_method japanese_to_s
My Qi4j version is built using three mixins, one for PersonState and
two different for the getFullName behavior. Structurally it is very
similar to the Hello World-tutorial. What I'd like to do is decide at
runtime which of the two behavioral mixins to use when building the
composite. Eg:
SingletonAssembler assembly = new SingletonAssembler() {
public void assemble(ModuleAssembly module) throws
AssemblyException {
module.addComposites(PersonComposite.class);
}
};
CompositeBuilderFactory builderFactory =
assembly.compositeBuilderFactory();
CompositeBuilder<PersonComposite> builder =
builderFactory.newCompositeBuilder(PersonComposite.class);
builder.stateOfComposite().firstName().set("Nancy");
builder.stateOfComposite().lastName().set("Carthwright");
PersonComposite nancy_carthwright = builder.newInstance();
Assert.assertThat(nancy_carthwright.fullName(),
equalTo("Nancy Carthwright"));
// I need some way to change the mixin used by the
CompositeBuilder<PersonComposite> builder...
PersonComposite nancy_carthwright2 = builder.newInstance();
// So that the firstName and lastName values are printed in the
opposite order.
Assert.assertThat(nancy_carthwright2.fullName(),
equalTo("Carthwright Nancy"));
Is there an easy way (not so easy ways are also fine :-) to achieve
this?
Cheers,
Anders Norås | http://andersnoras.com
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev