Reputation: 100378
I have a library project which contains an abstract class, let's say ClassA
. In the project that uses that library project, I have ClassB
that extends 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
The reason I'm asking is that I'm creating multiple applications, which only have different methods in ClassB
. Therefore I'm creating a library that all of these applications can use, only having to extend ClassA
. These applications are separate projects, using the library.
Upvotes: 0
Views: 97
Reputation: 11522
What you're looking for is something like the Abstract Factory pattern. The application code (the code that calls into the library) would, at some point, need to pass in a Factory class that would be used by the library to create instances of ClassA objects. In your case, the Factory class would generate instances of ClassB.
Depending on the design and functionality of the classes, it's likely that ClassA should be redesigned as an interface, or at least as an abstract class, as part of this refactoring.
EDIT: Here's an untested pseudo example:
This code would be in the library
class INeedAFactory {
public Interface MyFactory {
public ClassA makeClassAInstance();
}
private MyFactory m_factory;
public registerFactory(MyFactory factory) {
m_factory = factory;
}
private ClassA makeClassAInstance() {
// m_factory had better not be null!
return m_factory.makeClassAInstance();
}
private void ClassAConsumer() {
ClassA classA = makeClassAInstance();
// ... etc. ...
}
}
This code would be in the application:
class LibraryPatron {
class MakeClassB implements INeedAFactory.MyFactory {
public ClassA makeClassAInstance() {
return new ClassB();
}
}
public LibraryPatron() {
INeedAFactory libraryObject = new INeedAFactory();
libraryObject.registerFactory(new MakeClassB());
// ... etc...
}
}
Upvotes: 1
Reputation: 71
I feel sick when I write this, because your design is flawed, but you asked how you would do it....so here it goes:
Class<?> bClass = Thread.currentThread().getContextClassLoader().loadClass("com.foo.ClassB");
Then you can do something like:
if (object.getClass() == bClass) { ... }
I would seriously recommend coming up with a better solution however. Try writing your code in one project then re-factor out common things that can make up your "library".
Upvotes: 0
Reputation: 15855
So you're writing an abstract class and you want it to be able to get a hold of all objects of unknown sub-classes of that class? It sounds to me like you're wanting an Aspect-Oriented management like AspectJ or PicoContainer.
Upvotes: 0
Reputation: 14053
It isn't really clear, but as I understand it if you want to get an instance of the ClassB
you just need to do a new ClassB()
...
Upvotes: 0
Reputation: 7691
If your library needs to use ClassB, then ClassB should be part of your library. Your library shouldn't need to know about classes in projects which use it.
If your library just needs a ClassA, and you've got a ClassB, you can cast your object to a ClassA before you pass it in, like this:
ClassB b = new ClassB();
ClassA a = (ClassA)b;
// pass a into your library
Upvotes: 1
Reputation: 8044
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.
Upvotes: 0