How to build a dependency injection framework by myself?

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

Hi,
I am using a factory class to build instances. the factory method looks
like:

<code>
class MyFactory{
Item getItem(String key){
...}
}
</code>

and the key and class are configured in a file
key=com.abcd.ClassA
b=com.abcd.B

<code>Item</code> is an interface implemented by some of the above
classes mentioned inside the cofiguration file.

I want the factory class to read the configuration file and then
instantiate the given class and as a return value. But I have no idea
how to instantiate the class. I tried Class.forName() and seesm it is
not a good solution to this problem.
 
A

Andrew Thompson

moop™ wrote:

Sub: How to build a dependency injection framework by myself?

<quips>
A general answer to ..
"How to xxx a yyy by myself?"
...might start with..

1) Don't request help from other people.
...
</quips>

...oh, and I don't know what the experts on this
sort of stuff will say, but it seems unlikely that
it will be possible to do 'plug-ins' without at
least a little classloader trickery..

Andrew T.
 
C

Chris Uppal

moopT said:
I want the factory class to read the configuration file and then
instantiate the given class and as a return value. But I have no idea
how to instantiate the class. I tried Class.forName() and seesm it is
not a good solution to this problem.

Assuming you want to do this yourself in order to learn about the relevant
techniques, why not start by looking at something small, such as picocontainer,
and see what they do ? Then read up on the classes, methods, and techniques
they use.

By all means ask more questions as you learn, but this isn't really the sort of
forum for a full description of the architectural options and challenges.

Also, a related question comes up fairly often here. Try checking Google's
archives for posts about "pluggable" components.

-- chris
 
A

AndrewMcDonagh

moop™ said:
Hi,
I am using a factory class to build instances. the factory method looks
like:

<code>
class MyFactory{
Item getItem(String key){
..}
}
</code>

and the key and class are configured in a file
key=com.abcd.ClassA
b=com.abcd.B

<code>Item</code> is an interface implemented by some of the above
classes mentioned inside the cofiguration file.

I want the factory class to read the configuration file and then
instantiate the given class and as a return value. But I have no idea
how to instantiate the class. I tried Class.forName() and seesm it is
not a good solution to this problem.

why on earth bother?

just download PicoContainer - it does this exact thing and more and is
fully tested and proved to work.
 
P

PerfectDayToChaseTornados

moopT said:
Hi,
I am using a factory class to build instances. the factory method looks
like:

<code>
class MyFactory{
Item getItem(String key){
..}
}
</code>

and the key and class are configured in a file
key=com.abcd.ClassA
b=com.abcd.B

<code>Item</code> is an interface implemented by some of the above
classes mentioned inside the cofiguration file.

I want the factory class to read the configuration file and then
instantiate the given class and as a return value. But I have no idea
how to instantiate the class. I tried Class.forName() and seesm it is
not a good solution to this problem.

Download Spring http://www.springframework.org/download, look at the source
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top