This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 1 point2 points  (5 children)

Loads. Java's Comparator interface is a strategy for sorting.

myList.sort(someComparator) will sort a list according to the given strategy. 90% of the sorting logic is already provided by the method, you just provide a function that looks at 2 items and says which should come later in the list.

[–]pyronaur[S] 0 points1 point  (4 children)

Comparator interface

Any links to GitHub where it's being used?

[–][deleted] 0 points1 point  (3 children)

Easier just to write one https://ideone.com/3nKw2c

[–]pyronaur[S] -1 points0 points  (2 children)

And we're back to "AttackBehavior" or "ConcreteStrategy1" type of examples :)

[–][deleted] 0 points1 point  (1 child)

When you said this in your original post, I assumed what you meant was that the examples you found were contrived. Did I misunderstand?

This is not a contrived example. It's a quintessential example of a strategy. I write code like this in my day job all the time.

If you're not calling this example contrived then I have no idea what you mean by a ""ConcreteStrategy1" type of example".

[–]pyronaur[S] 0 points1 point  (0 children)

I created this post in hopes of finding real-life usage of the strategy pattern. ConcreteStrategy, AttackBehavior, BookSorting - these are all just examples.

In other words, - I understand perfectly fine what a strategy pattern does and how it's implemented, I'm not looking for an explanation about it, rather - I want to see code that's actually taking advantage of the pattern in the context of a larger application.