动态类派生 - 访问者模式
我想以这种方式创建访问者模式
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在寻找一种从类对象创建类实例的方法,请查看 java 反射 api。
或者
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.
or