Introduction

Thinking in Dobo (Define Object By cOntext) is about understanding that each object can have different behavior in different context.Terminology :

  • The Client Client is the object which require Interface of Dobo.
  • The Interface of Dobo Interface of Dobo is the interface which can accessed by the client.Eg : Thread can use Runnable, if we define Dobo for Runnable then Runnable is the Interface of Dobo. (interface of dobo also known as ContextInterface ).
  • The Context. The Context is the annotation which bind the Interface of Dobo( ContextInterface ) .
  • The Dobo The Dobo is the object which annotated with context. The dobo object also known as ContextObject

Sample of Dobo

Dobo in Simple Java

  • The Client : java.lang.Thread
  • The Interface of Dobo : java.lang.Runnable
  • The Context : dobo.sample.RunnableContext dobo.sample.RunnableContext is a Context, in essence it is an annotation that we need to create or generated for dobo to bind java.lang.Runnable into Dobo Object.
    							
    @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 { }
    
    }
    							
    						
  • The Dobo : dobo.sample.HelloDobo HelloDobo is the object which is Dobo.
    						
    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(); 
    }
    							
    						

Dobo in Spring Framework

  • The Client : springframework
  • The Interface of Dobo : org.springframework.web.servlet.mvc.Controller
  • The Context : dobo.sample.RunnableContext dobo.sample.ControllerContext is a Context, in essence it is an annotation that we need to create or generated for dobo to bind java.lang.Runnable into Dobo Object.
    							
    @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{}
    
    }
    							
    						
  • The Dobo : dobo.sample.ControllerObject ControllerObject is the object which is Dobo.
    						
    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>
    
    
    						

Dobo in Google Guice

  • The Client : googleguice
  • The Interface of Dobo : dobo.sample.HelloWorld
  • 					
    import java.io.PrintStream;
    
    public interface HelloWorld { 
    	public void setStringToPrint(String toBePrinted); 
    	public boolean sayHello(PrintStream printStream); 
    }
    						
    					
  • The Context : dobo.sample.HelloWorldContext
    							
    @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{}
    
    }
    							
    						
  • The Dobo : dobo.sample.HelloWorldObject
    							
    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; 
    	}
    }
    						
    						
    The guice main
    							
    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");
    	}
    }
    
    
    						

Generating The Context

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.

Generating The Context in the IDE

Generating context using IDE, you need to run

java net.sf.dobo.Dobo

See in console window, and follow the questions.