Originally posted by Shuai Liu:
Let's say I have an interface
and in my user class, I want to call getSomething in IEnvironmentVariable(directly or indirectly) without knowing which implementation is called.
...
I can't set the actual concrete class by using myMethod(MyInteface param) because I don't have any knowledge of the concrete class to be created.
Does that sound crazy?
Richard
N 37 33 W 122 18
The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Shuai Liu:
Richard, if I understood you correctly, I will have a factory like this:
and the author can change the concrete class ( EnvironmentVariableImpl1 in this case) being returned to the user.
But my real problem is: in my factory class, I don't know what concrete class to create, so it would look like
so that I don't need to change the getEnvironmentVariable() method in MyEnvironmentVariableFactory class, and it will return the current available implementation of IEnvironmentVariable.
I want MyEnvironmentVariableFactory be able to pick up the available implementaion itself at run time.
I assume that one way I can do it is that I specifiy the classname of the implementation in some sort of property file that my Factory class can read and use reflection to create the concrete class on the fly.
Richard
N 37 33 W 122 18
Originally posted by Richard Jensen:
Are the classes that implement the interface dynamic or fixed? That is, do you know all the possible types when you code the factory class or do you have to allow for an unknown class to be used at runtime? Reflection handles the latter case, but can be a bit tricky.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |