File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Runtime Polymorphism using spring" Watch "Runtime Polymorphism using spring" New topic

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){
obj = new A();
} else {
obj = new B();

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: 607

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:
subject: Runtime Polymorphism using spring
jQuery in Action, 3rd edition