Reputation
616
Top tag
Next privilege 1,000 Rep.
See votes, expandable usercard
Badges
5 8
Newest
 Commentator
Impact
~21k people reached

  • 0 posts edited
  • 0 helpful flags
  • 24 votes cast
May
25
awarded  Commentator
May
25
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Sher10ck I'd not call it a God Class, it still has a well defined role, and adheres to the single responsibility principle. Having said that, the aim here was to keep the structure similar to the original code, while avoiding some of the problems of direct casting etc. Once the visitor starts to become unwieldy you need to switch to another pattern. In this case I'd likely convert the visitor to a Factory for Adaptor style objects - with each Adaptor implementing only one public function.
May
6
awarded  Good Answer
May
6
awarded  Yearling
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine, if you're happy to add that complete example into the end of the answer that might help others.
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine that looks good, but since your functions dont take arguments, instead of sub1 -> sub1.makeASandwich() you can just use Sub1::makeASandwich - which is even neater. (Thats not the case in the original example, so I had to stick with the long form lambda, rather than the method reference)
May
6
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
Changed `=>` to `->`.
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine - stream() is part of the new java 8 streaming protocol. The forEach(...) takes a java 8 lambda and applies it to each of the elements of the stream. BaseVisitor.forEach(...) is a function you'd need to create that takes two java 8 lambdas of different types (one taking Sub1, the other taking Sub2) and returning a single lambda that takes a Base and applies the correct lambda using the visitor patter.
May
1
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Radiodef An advantage of using the visitor pattern over switching on type directly is that once you've added onSub3 to the visitor interface, you get a compile error in every location you create a new Visitor that hasn't yet been updated. In contrast switching on type will at best generate a run-time error - which can be trickier to trigger.
Apr
30
awarded  Nice Answer
Apr
30
awarded  Yearling
Apr
30
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Mankarse A little syntactic sugar can make it very far from ugly - I've updated it with an example.
Apr
30
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
added 596 characters in body
Apr
30
awarded  Editor
Apr
30
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
added 596 characters in body
Apr
30
answered Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
Feb
16
answered Why is base-for-all-objects discouraged in C++
Jan
27
answered Is the 'finally' portion of a 'try … catch … finally' construct even necessary?
Jan
27
comment Is the 'finally' portion of a 'try … catch … finally' construct even necessary?
Perhaps even more common is returning inside the try block, rather than inside the catch block.
Sep
1
comment What's wrong with comments that explain complex code?
If a developer I was working with simply wrote "evil floating point bit level hacking" to explain the fast square-root algorithm - they'd get a talking to by me. So long as they included a reference to somewhere more useful I'd be happy though.