Talk:Strategy pattern

message: Except of simplest toy cases, the Strategy usually needs to access the data of the Context. For example some braking algorithms may need to access car properties like car.speed, car.brake_fluid_level etc.. GoF book suggests different ways of achieving this:
# context passes the data to the strategy operations
# context passes itself as an argument to the strategy operations
# strategy stores a reference to the context, eliminating the need to pass anything at all
In case of (2) and (3), the Context must define an interface for accessing its data.
— Preceding unsigned comment added by 86.2.5.138 (talk) 21:09, 27 June 2015 (UTC)
thread_title:  Examples do not illustrate the interaction of the Strategy with the Context 

message: Does anyone else think these examples don't exactly convey the encapsulation and alternating of algorithms? All I see are some roundabout "switch" statements. Perhaps if the algorithm parts of the examples weren't so throwaway, ie: instead of just tracing "Doing the task", the interfaces actually implement more functions and the article walk us through a simple yet concrete example. Perhaps the traversal of trees with different iteration algorithms - that would be a good, useful, simple example.
Agreed.  I personally did not understand the article fully due to the lack of meaningful examples.  Assuming I'm not totally wrong in my understanding, would a set of sorting algorithms perhaps be a better example?  At run-time, the algorithm to call may then be chosen based on whether the data to sort is expected to be mostly disordered (e.g. quicksort), mostly ordered, mostly disordered and generated on the fly (e.g. treesort), low in quantity (e.g. bubblesort), etc.....
Alternatively, having differently optimised versions of the same algorithm, and deciding which to call at run-time based on the hardware available.  For example, matrix multiplication using either FPU/MMX/SSE*/AVX, etc.
176.253.193.141 (talk) 19:12, 28 March 2013 (UTC)
thread_title:  Poor Examples 

message: I always wonder why for design patterns on wikipedia, why not choose one example, make the UML diagram
for it and implement it in multiple languages, instead of having a different example in each language. 
That way the reader can compare the examples if he/she doesn't understand. It's almost the case here, most 
examples are the same, but not the UML and the Python example
Gabriel - June 10th 2008  —Preceding unsigned comment added by 83.219.124.13 (talk) 08:35, 10 June 2008 (UTC)
thread_title:  Why not code the same example, in multiple languages? 

message: ...
button1 = Button(sum, "Add 'em") - shouldn't this line pass in function liek the line below?
button2 = Button(lambda nums: " ".join(map(str, nums)), "Join 'em")
...  —Preceding unsigned comment added by Markscottjohnson (talk • contribs) 10:22, 15 February 2008 (UTC)
replies: text: sum is a built-in Python function, so there's no bug there. --MichalKwiatkowski (talk) 16:20, 17 February 2008 (UTC)
thread_title:  Error in python code? 

message: I do not like the war comparison, I'd rather describe the same thing as a gaming situation. Somebody else has an opinion about that? --Bjoern.thalheim 09:36, 15 November 2005 (UTC)
thread_title:  War 

message: The example says that it is in C#. Would it be possible to do this pattern in C# using delegates, and if so, should that be included in the article? The two concepts seem closely related at first glance. 168.209.98.35 15:17, 18 February 2006 (UTC)
thread_title:  Strategy Pattern using delegates in C#? 

message: I think it would be better to have the code example in a more "traditional" language, like C++.
Since there are more people knowing C++ as opposed to C#, it would reach more people.
:Definitely agreed 144.173.6.66 16:11, 3 June 2006 (UTC)
thread_title:  C#?? 

message: An example needs to be a concrete realization of what the abstract idea is capable of.  Here we're shown an example of how a "strategy" can perform different "executions" depending on the "context," but the example entities are named Strategy, Execute, and Context.  That's hardly a concrete realization of this concept.
Plus, the code itself seems too trivial to get the point of the object across.  Three different contexts are created, and their execute methods are called.  This code easily devolves into just calling three different methods.  An example where the strategy design pattern actually *improves* upon a simpler design, rather than needlessly complicating it (due to the sheer simplicity and contrived-ness of this code), would be better.
The class diagram spells "concrete" incorrectly.  — Preceding unsigned comment added by 194.201.25.22 (talk) 11:12, 28 June 2011 (UTC)
thread_title:  Example not concrete 

message: C# interfaces should always be named with a prefixed I such as IObserver.
This is not true. It is a common convention though. travelan (talk) 11:32, 26 January 2009 (UTC)
thread_title:  IInterface 

message: I am currently in the process of adding formatted C++ code for Design Patterns. An example of the style can be seen in this topic: Bridge pattern. The code for this topic is currently being reviewed.
EDIT 21:25, 12 July 2006 (UTC)
thread_title:  C++ code: adding later 

message: I've reinserted and reformatted the C# code that was deleted.  Santorumm, what basic structures that you indicated in your edit summary does C# lack that are present in C++?  I'll see if I can add a C# delegate example and pair up header files with their .cpp counterparts later.  Also, Java is similar enough to C# that a separate Java example would be superfluous.  Supadawg (talk • contribs) 02:42, 1 September 2006 (UTC)
replies: text: It is missing things such as ArrayList and LinkedList containers, which would make a simple example illustration of the fundamental pattern difficult.. Santorummm 15:23, 1 September 2006 (UTC)
replies: text: I don't mean to sound rude, but have you ever programmed in C# before?  The ArrayList is one of the most commonly-used classes.  Also, it's implemented as a linked list, making a separate class redundant.  Why do you need them, anyway?  The C++ example uses plain C-style arrays, which C# has also, which would be faster. Supadawg (talk • contribs) 20:27, 1 September 2006 (UTC)
replies: text: On certain code being superfluos: where's the limit? Give me (and many other coders offcourse) a proper UML presentation and I won't need code examples at all. This may not be the case in all possible cases but I hope the point is made. Or give me code in C# (for example) only, and I won't be needing Java or C++ code at all to port the code. So once again, I wonder where the limit is. Vladimir Bosnjak 20:32, 22 September 2006 (UTC)
thread_title:  Deleted C# code 

message: The current syntax coloring in the C++ code makes it impossible to edit, and only a little easier to read.  I think it should be removed. Supadawg (talk • contribs) 16:20, 4 September 2006 (UTC)
replies: text: I'll give it another couple of days, then remove it if no one replies. Supadawg (talk • contribs) 20:37, 6 September 2006 (UTC)
replies: text: No objections, so I'll go ahead and remove the syntax coloring. Supadawg (talk • contribs) 00:48, 10 September 2006 (UTC)
replies: text: No problem. My upcoming snippets will not be html formatted anymore since you made a valid point. I'll use the ANSI style formatting only (tabs, indents etc).You may be interested in removing the coloring for the Template Method pattern as well. Just preserve ansi style, 2 space tabs please. Vladimir Bosnjak 18:52, 22 September 2006 (UTC)
thread_title:  Syntax coloring 

message: I believe that the samples of this pattern in as many languages as desired should be allowed. However, I believe that one stipulation should be adhered to. All code sample should be as high a quality as possible and as concise as possible.
On top of that, I notice that some of the Design Pattern pages have no examples at all. (Feel free to move this thread to a more general article.) Let's try to have at least one example or two examples on each page -- preferably one in a language without first-class functions, and one in a language with them. Here's a humble recommendation...
High-priority:
* Java or C# -- whichever gets written first; they're both good examples.
* Python or Ruby -- both are popular, look like pseudocode, and have first-class functions.
Nice to have:
* JavaScript/ECMAScript -- Web programmers deserve a chance to educate themselves, too
Lower priority:
* The other of C# or Java that wasn't already implemented
* C++ -- the original book used C++ examples... so why duplicate the effort? Plus, a real-world, secure C++ example suitable for copy-and-paste often looks much different from a contrived, educational, naive implementation.
Bottom priority:
* Languages without built-in constructs for OOP
* Proprietary languages (e.g. Actionscript -- just use JavaScript!) -- there's usually an equivalent in the top 20 of the Tiobe language popularity index.
* Languages not in the Tiobe top 20
Style guidelines:
* Do something slightly different in each example -- don't directly translate.
* Try to avoid distracting language features. (My Python example uses a built-in function (sum) and lambda+map, which is brutal, but it does save a couple of trivial function definitions...)
* Minimize whitespace and newlines where it doesn't hurt clarity. It's nice if it fits on one screen.
* Indicate where you would encounter this pattern in a real program, if possible. Name variables accordingly.
* Test the example first, and include the output in the comments
— EricTalevich (talk) 19:54, 29 November 2007 (UTC)
thread_title:  Language Wars 

message: I've seen very different viewpoints on the internet about how the Strategy pattern should be used.
Some seem to say to only use the Strategy pattern as long as each algorithm produces the same output.  Others say that the Strategy pattern can/should be used for any variety of algorithms that may be run on an object, even if a given pair of algorithms may produce a different output.
: Different strategies will in general prodice different outputs. That's one reason why one would want to be able to change strategies. Performance, as you discuss below, or memory utilization may be other reasons. --71.214.221.153 (talk) 22:19, 27 March 2010 (UTC)
thread_title:  Strategy for Algorithms with different outputs? 

message: This pattern is invisible in languages with first-class functions.
In general one declares an Interface, not just a function, that defines a set of functions a strategy must implement.  See for instance, the sizers in wxWidgets. I don't see how a language that has first-class functions makes this invisible. Justify the claim with a reliable reference. I doubt you'll find one.  --71.214.221.153 (talk) 22:12, 27 March 2010 (UTC)
thread_title:  Invisibility claim for first class functions 

message: I'm not sure why it is here?--Michael miceli (talk) 20:58, 9 April 2008 (UTC)
thread_title:  What is actionscript doing here?! 

message: Aren't these two basically the same?
138.246.7.155 (talk) 13:12, 6 November 2008 (UTC)
thread_title:  Overlaps with [[Delegation pattern]] 

message: It seems to me that something important is glossed over in the examples.  All the examples (as far as I can tell) can be implemented using "simple" (C-like) function pointers; that is, the strategies don't hold any context about state.  This makes, for example, the C++ version look overly complicated for the behaviour granted, as the function pointer version can drop all the strategy class definitions and initializations.  The advantage of using a class or closure is that the strategy can store state.  —Preceding unsigned comment added by 86.22.75.44 (talk) 20:57, 21 February 2010 (UTC)
replies: text: You are correct.  The examples don't really do justice to the pattern. The article should be reworked.
--71.214.221.153 (talk) 22:22, 27 March 2010 (UTC)
replies: text: Note in the same respect, that it may be claimed to use the Strategy pattern in a functional programming style. See for example : Functional Programming in Java, Harnessing the Power of Java 8 Lambda Expressions (ch. 4, passim and page 66), by Venkar Subramaniam, where the pattern is 
claimed to be in use in cases where a lambda is passed in to some function using Java Stream. Ptyxs (talk) 17:38, 19 December 2014 (UTC)
thread_title: Closures

message: An interesting criticism of this pattern:
http://coderoom.wordpress.com/2010/06/23/criminal-overengineering/
I don't know whether the criticism is valid (I program microcontrollers with 128 bytes of RAM, so most design patterns simply don't apply) but I couldn't help noticing that none of the five or so Wikipedia pages on design patterns I checked has anything bad to say about them.  Guy Macon  00:42, 24 June 2010 (UTC)
thread_title:  Criticism? 

message: Ruby example is not thread safe, is it?
self.class.send :include, strategy
this makes it switch strategy in all objects, in all threads...  —Preceding unsigned comment added by 87.119.57.129 (talk) 09:32, 28 June 2010 (UTC)
thread_title:  Ruby example not thread safe, also kind of ugly 

message: It would be nice to see a section discussing the context object and what it is for. (I am not sure). It seems to be related to the need for some strategy patterns to store information. Aka some algorithms have a memory, and some don't. It seems to be that this is what the Context is utilized for, but a section on this distinction with some external references to algorithms that use or don't use memory would be nice. I believe the Gang of Four book has a few examples that attempt to apply different line formatting examples to a word document. Sorry for poor grammar, in a hurry. Crabpot8 (talk) 19:35, 19 July 2010 (UTC)
thread_title:  Purpose of context in strategy pattern is unclear 

message: While searching for this pattern, I initially looked for "applying criteria" and found this article https://en.wikipedia.org/wiki/Criteria_Pattern. it seems to be a duplicate of this one and has only minor differencies (filtering and selecting instead of filtering only). it was written by one guy who cited himself as the author of the pattern and linked to some article from 2011 (see history). could someone more familiar with patterns look into this and create a redirect to this article or improve the other article, please? 109.192.57.59 (talk) 22:32, 29 September 2014 (UTC)
thread_title:  Possible duplicate: Criteria pattern 

message: I think it would be better if the Java example had a strategy that effected the add() function not the billing function  — Preceding unsigned comment added by 84.94.147.201 (talk) 15:11, 26 May 2015 (UTC)
thread_title:  Improve Java Example 

message: I would like to share the following UML diagrams I have published on  Design Patterns Open Online Learning. Your comments are welcomed.
In the above UML class diagram, the Context class doesn't implement an algorithm directly.
Instead, Context refers to the Strategy interface for performing an algorithm (strategy.algorithm()), which makes the Context independent of how an algorithm is implemented.
The Strategy1 and Strategy2 classes implement the Strategy interface, that is, implement (encapsulate) an algorithm.
The UML sequence diagram
shows the run-time interactions: The Context object delegates an algorithm to (switches between) different Strategy objects. First, Context calls algorithm() on a Strategy1 object,
which performs the algorithm and returns the result to Context. 
Thereafter, Context calls algorithm() on a Strategy2 object,
which performs the algorithm and returns the result to Context.
Vanderjoe (talk) 19:03, 7 July 2017 (UTC)
thread_title:  UML class and sequence diagram 

message: The composition on the left hand side should be an aggregation.
1. The strategies can exist without the context (so no existential dependency)
2. The strategies can be used in other contexts (so no strong ownership)
3. The original source [Gamma et al.] says so  — Preceding unsigned comment added by 134.106.27.226 (talk) 11:32, 19 April 2018 (UTC)
thread_title:  Composition in UML Class diagram should be aggregation