定义抽象对象的实现 [英] Define Implementation for abstract Object
问题描述
我正在寻找一种执行以下操作的方法:
I am looking for a way to do the following:
A项目:
定义一个抽象类,在发生某些事件时会调用该抽象类(事件处理程序)
定义将使用上面的事件处理程序触发事件的引擎
A Project : Defines an abstract class that is called when some events happen (event handler if you will) Defines the engine that will fire the events using the event handler above
B项目:
定义抽象类的实现
运行引擎。
B Project: Defines the implementation for the abstract class Runs the engine.
我如何注册实现类,并确保该类在引擎运行时被调用。
How can i register the implementation class and make sure that is the one being called when the engine runs.
编辑1:通过寄存器,我的意思是我必须以某种方式定义应该为给定抽象对象调用的实现
EDIT 1: By register i mean i must somehow define which is the implementation that should be called for that given abstract object
对不起,如果问题不是很清楚,如果需要更多详细信息,请告诉我
Sorry if the question isn't too clear, let me know if you need some more details
推荐答案
在运行时,您可以使用名称在您的A项目中传递的实现(带有属性文件或Java系统属性)。
然后,您可以使用 class.forName()
在类路径中找到此类,并使用 newInstance()
。
At runtime, you can have the name of the implementation passed in your A project (with a properties file or a Java system property).
Then you find this class in the classpath with class.forName()
and instantiate it with newInstance()
.
但是您更喜欢使用 Guice 或Spring,它可以让您以干净的方式将东西粘在一起。
But you'd prefer using a framework like Guice or Spring, that will allow you to glue stuff together in a clean way.
这篇关于定义抽象对象的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!