English 中文(简体)
寻找某种模式
原标题:Looking for some sort of pattern
  • 时间:2011-06-20 20:37:14
  •  标签:
  • java
  • android

我有一个图书馆项目,有一个抽象的班子,请说<代码>ClassA。 在利用该图书馆项目的项目中,我有<代码>ClassB,该编码可扩展<代码>ClassA。

Now here s my problem. In the library project I want to use an instance of the implemented ClassB, but I have no idea how to retrieve that instance. Is there any pattern or other ideas for this?

Here s a simple diagram of the situation.

<><>Edit>/strong>

I m询问的理由是,Im制造多种应用,在<代码>中,这种应用方法只有不同的。 类别B:。 因此,我制作一个图书馆,供所有这些应用程序使用,但只需要扩展<条码>。 这些申请是利用图书馆进行的单独项目。

最佳回答
问题回答

如果贵图书馆需要使用B级,那么B级应当成为你的图书馆的一部分。 你的图书馆没有必要了解使用该图书馆的项目中的班级。

如果贵图书馆只需要一等A级,而且你可以打上一门B级,那么你就可以把物体投到一门B级。 随函附上:

ClassB b = new ClassB();
ClassA a = (ClassA)b;
// pass a into your library

Move class B into the same library as class A. Your project already depends on the library so it will still be able to reference class B but class B will also now be available to other classes within the library.

这确实不明确,但正如我理解的那样,如果你想得到<编码>ClassB的事例,你刚刚需要做一个<代码>的新类别B(......。

在我写这封信时,我感到生病,因为你的设计有缺陷,但你询问你会怎样做。

Class<?> bClass = Thread.currentThread().getContextClassLoader().loadClass("com.foo.ClassB");

然后,你可以做这样的事情:

if (object.getClass() == bClass) { ... }

不过,我非常建议拿出更好的解决办法。 在一个项目中撰写你的代码,然后重新列出能够构成你的“图书馆”的共同内容。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签