aspose file tools*
The moose likes Spring and the fly likes Runtime Polymorphism using spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Runtime Polymorphism using spring" Watch "Runtime Polymorphism using spring" New topic
Author

Runtime Polymorphism using spring

Ashish Malik
Ranch Hand

Joined: Jul 11, 2010
Posts: 50
I have:

interface I { someMethod(); }

class A implements I { someMethod(){} }
class B implements I { someMethod(){} }

In some class i want to do:

class Logic {
I obj;

run(boolean x){
if(x){
obj = new A();
} else {
obj = new B();
}
obj.someMethod();
}

I can do this easily with plain Java but, i how to do using spring?
Before that, is there any design problem with that code?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

You can do this with Spring, since it's designed to interface and you can select the implementation based on logic. You can do it in multiple ways - so not sure what your stuck at or how you are trying to use Spring.

Usually you might want to encapsulate object creation in a factory Factory method pattern.
Again without looking at the specific context it's not easy to make a general statement on whether something is a good or bad idea!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime Polymorphism using spring