Thinking in Dobo (Define Object By cOntext) is about understanding that each object can have different behavior in different context.Terminology :
@Context(java.lang.Runnable.class) // here we define that this annotation is a Context of Runnable (MANDATORY) public @interface RunnableContext { // the name of the context is RunnableContext, we can put a different name as we like /* * Here we define a Context member, which will bind to method name run in the interface */ @Target(ElementType.METHOD) // this annotation is for method (MANDATORY) @Retention(RetentionPolicy.RUNTIME) // this annotation can be read at runtime (MANDATORY) @ContextMemberType(void.class) // this annotation means that method run will return void (note : for void return type it is optional to define @ContextMemberType) @ContextMemberMethod(name = "run", parameterType = {}) // This annotation will be bind to a method name "run" which has empty "parameter" of the interface java.lang.Runnable public @interface run { } }
public class HelloDobo { @RunnableContext.run public void helloDobo(){ System.out.println("Hello Dobo"); } }
The main
public static void main(String [] args){ Thread t = new Thread((Runnable) Dobo.instantiate(new HelloDobo(),RunnableContext.class)); t.start(); }
@Context(org.springframework.web.servlet.mvc.Controller.class) public @interface ControllerContext{ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @ContextMemberType(org.springframework.web.servlet.ModelAndView.class) @ContextMemberMethod(name="handleRequest",parameterType={javax.servlet.http.HttpServletRequest.class,javax.servlet.http.HttpServletResponse.class}) public @interface handleRequest{} }
public class ControllerObject { @ControllerContext.handleRequest public ModelAndView doSomeAction(HttpServletRequest request,HttpServletResponse response){ return new ModelAndView("hello"); } }
The spring-xml
<bean id="controllerObject" class="dobo.sample.ControllerObject"/> <bean id="springappController" class="net.sf.dobo.spring.DoboFactoryBean"> <property name="context" value="dobo.sample.ControllerContext"/> <property name="contextImplementationObject"><ref bean="controllerObject"/></property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.htm">springappController</prop> </props> </property> </bean>
import java.io.PrintStream; public interface HelloWorld { public void setStringToPrint(String toBePrinted); public boolean sayHello(PrintStream printStream); }
@Context(net.sf.dobo.sample.helloworld.HelloWorld.class) public @interface HelloWorldContext{ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @ContextMemberType(void.class) @ContextMemberMethod(name="setStringToPrint",parameterType={java.lang.String.class}) public @interface setStringToPrint{} @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @ContextMemberType(boolean.class) @ContextMemberMethod(name="sayHello",parameterType={java.io.PrintStream.class}) public @interface sayHello{} }
public class HelloWorldObject { private String toBePrinted; @HelloWorldContext.sayHello public boolean hello(PrintStream printStream) { printStream.print(toBePrinted); return true; } @HelloWorldContext.setStringToPrint public void setTheStringBaby(String toBePrinted) { this.toBePrinted = toBePrinted; } }
public class HelloWorldRunner { @Inject private HelloWorld helloWorld; public void sayHello(String nameToPrint){ helloWorld.setStringToPrint(nameToPrint); helloWorld.sayHello(System.out); } public static void main(String [] args){ Injector injector = Guice.createInjector(new AbstractModule(){ public void configure() { bind(HelloWorld.class).toProvider(new DoboProvider<HelloWorld>(new HelloWorldObject(),HelloWorldContext.class)); } }); HelloWorldRunner client = new HelloWorldRunner(); injector.injectMembers(client); client.sayHello("Hello World"); } }
Creating the Context is troublesome if you don't have much time, and dealing with deadline. However dobo comes with generator to generate the context. You can specify the context name and package, and after that you can refactor the name to what ever you like. In order to generate Context all you have to do is.
java -jar dobo-X.X.jar
Don't forget to include required classpath.