开发者

Class derivation on the fly - Visitor Pattern

I would like to create visitor pattern in such a way

public interface Visitable<T>{
   public void accept(T visitor);
}

public interface SomeBusinessService implements Visitable<SomeVisitor>{

  public void mtd1();
  public void mtd2();
}

public abstract class SomeBusinessBean1 implements SomeBusinessService {
   public void mtd1(){}
   public void mtd2(){}
} 

public abstract class SomeBusinessBean2 implements SomeBusinessService {
   ...
}

and so on

then I would like to create a factory

public class SomeBusinessServiceF开发者_如何转开发actory {
   public SomeBusinessService createService
                 (Class<? extends SomeBusinessService> clazz ){
      //do some stuff to create appropriate class derivation on the fly
     // that will have accept() method implemented 
   }
}

and I could invoke it in the following way

SomeBusinessService  service = 
    SomeBusinessServiceFactory.createService(SomeBusinessBean1.class);

With this approach I would't have to create comman abstract class for all beans that implement Visitor interface accept() method.

This solution could also be used in situations where we would like to have a common behaviour of specific methods depending on service factory per class hierarchy.

Is there any way to do that with standard jdk or maybe I need to use external tools like cglib or maybe what I'm saying is rubbish and we can achive that goal in some other way.

Tx in advanced


If you are looking for a way to create a class instance from its class object than have a look at the java reflection api.

clazz.newInstanze();

or

clazz.getConstructors(...).newInstance(...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜