定义抽象对象的实现 [英] Define Implementation for abstract Object

查看:27
本文介绍了定义抽象对象的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来执行以下操作:

I am looking for a way to do the following:

一个项目:定义在某些事件发生时调用的抽象类(如果您愿意,则为事件处理程序)定义将使用上面的事件处理程序触发事件的引擎

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆