• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime Polymorphism using spring

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic