• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

refactor Strategy Pattern code "functionally"?

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the context of this thread: Simple Use case: Get the list of Class name.
https://coderanch.com/t/678797/java/explanation-stream-reduce-parallelStream-reduce

I see n ways of of doing 1 same things for above use case. I choosed following 3 for learning purpose:

1. Way 1: Using toList collector.
2. Way 2: Using Stephen's way assuming toList collector does not exist (for the sake of learning.)
3. Way 3: Using original mine: parallel reduce (version corrected after a read of Stephen's explanation regarding threading issues)

I refactored the 3 specific ways of doing 1 same things.
I found that the code looks like a Strategy Pattern in action functionally.
See below code: Way 1.

Now: Is it safe to say that we can refactor Strategy Pattern code "functionally"?




 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A question based upon my below side notice:

Is the function interface java.util.function.Supplier the functionally way of creating new object?


2. Just a side notice:



While looking at above ways of creating an instance for a functional interface,
I encapsulate it into a factory as full code down below:

I see a readability? :-D



 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A side note, step-wise learning: To have client/caller controls the parallel switch

I changed the original from:

to:



 
I brought this back from the farm where they grow the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic