动态类派生 - 访问者模式

发布于 2024-10-15 07:55:04 字数 1167 浏览 3 评论 0原文

我想以这种方式创建访问者模式

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 {
   ...
}

,依此类推,

然后我想创建一个工厂

public class SomeBusinessServiceFactory {
   public SomeBusinessService createService
                 (Class<? extends SomeBusinessService> clazz ){
      //do some stuff to create appropriate class derivation on the fly
     // that will have accept() method implemented 
   }
}

,我可以通过以下方式调用它

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

使用这种方法,我不必为所有 bean 创建comman抽象类 实现 Visitor 接口的accept()方法。

该解决方案还可以用于我们希望根据每个类层次结构的服务工厂实现特定方法的共同行为的情况。

有什么方法可以使用标准 jdk 来做到这一点,或者我可能需要使用 cglib 之类的外部工具,或者我所说的可能是垃圾,我们可以通过其他方式实现该目标。

提前发送

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 SomeBusinessServiceFactory {
   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-10-22 07:55:04

如果您正在寻找一种从类对象创建类实例的方法,请查看 java 反射 api。

clazz.newInstanze();

或者

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

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(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文