Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Strategy Pattern With Generics

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am messing around with design patterns (I am still new) and I was trying to write some code to take advantage of generics.

I.E. I was wondering how someone might implement the strategy pattern if the return type on the interfaces method could be different. Some code would probably help explain.



Seems simple enough so far. But here is where I get stuck. How can I inject a concrete 'stuff' implementation into another class at runtime to get a certain behavior but still take advantage of type safety.



I have been thinking about this for a few days and I am really stumped. Can you use generics to inject a certain behavior at runtime.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it depends on what you're doing with the results.

For a start, you could also make the Context generic, and use the same generic type for your Stuff implementations. That would be suitable for some purposes, but not for others.

If that's no good - what is there in common with the way the context handles the results? If it can potentially do anything with it, I don't see how generics are going to be able to help you much. You can declare myStuff as a Stuff<?>, which would allow you to use any type (and still get type safety within the Stuff classes and maybe in other classes that use them). You might be able to use an upper- or lower-bound (? super Xxx or ? extends Yyy) if there's some common factor to the handling.

Does any of that help?


(By the way, there's another forum that I think this is better suited to, so I'll move it there).
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did think about a generic context but I was trying to avoid the user of the api to avoid having to know what type 'Stuff' was. But after you response and a few days of thinking I so do not think that there is any way around it.

Thanks so much for the response. And for moving post!!!
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic