Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hairy about generics (Challenge)

 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I was trying to create a code example for a (as I imagine) useful pattern I would like to call "friend pattern". The name of the pattern is inspired by the C++ "friend" keyword.

Imagine you have a bunch of classes that stores some data, each with different properties and you would like to create a editor (friend) for each of these classes. Of course, these classes has no business of knowing how they can be edited and we would like to define separate classes to edit them. The pattern I called "friend" pattern provides a way other programmers to add their own data classes and editors without having to rebuild any existing component.

I tried to use generics in my example code as much as possible but when I write the last two line I realized I am stuck. I didn't think this through and the last two line won't compile. I realized I might have to change quite a bit of code and remove some of the generics usage. I would really hate that because I find java's generic very self-documenting and make my idea easy to read and understand.

Can someone help me to make minimum number of changes and make the code compile?





thanks
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh by the way...
I just wanted to say I know why this code wouldn't compile, but I realize if I want to make the code compile I would have to remove a while bunch of generics and I want to minimize that
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found solution

line 40~43:




line 82~92

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic