• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic