File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime Polymorphism using spring

 
Ashish Malik
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic