Talk:Strategy pattern

From Wikipedia, the free encyclopedia

Contents

[edit] Why not code the same example, in multiple languages?

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)

[edit] Error in python code?

... 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)

sum is a built-in Python function, so there's no bug there. --MichalKwiatkowski (talk) 16:20, 17 February 2008 (UTC)

[edit] War

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)

The war is an example of a game.

[edit] Strategy Pattern using delegates in C#?

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)

[edit] C#??

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)

For most simple code if you understand C++ you understand C# but this doesn't happen in the opposite way so I think C# it's ok.

[edit] Example not concrete

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.

[edit] IInterface

C# interfaces should always be named with a prefixed I such as IObserver.

[edit] previous issues

C# vs. C++
the example should stay in C#. In the future, C++ will fade out as one of the top-liked languages. As i see many up-and-coming programmers, the tendency is towards high level, true OO languages like C# and Java. However, adding an additional example in C++ would, of course, not be useless.

Multiple instances
This example does not, at all, show the strategy design pattern. It shows a simple case of Realization (the UML relationship for implementing an interface). What would be useful is creating a mutator in the Context class:

//if i remember the C# syntax for accessor/mutator
protected Strategy Strategy{
  get { return strategy;  }
  set { strategy = value; }
}

so that way, you can keep one instance the Context class alive, and then dynamically set it's strategy. Then it would be the Strategy Design Pattern because it dynamically changes the algorithm the Execute() method executes.

[edit] C++ code: adding later

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)

Done, C++ code added.

Vladimir Bosnjak 14:31, 11 July 2006 (UTC)

[edit] Please add a Java example

A Java example could potentially add clarity to this subject. ThanksSantorummm 00:31, 1 September 2006 (UTC)

[edit] Deleted C# code

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 (talkcontribs) 02:42, 1 September 2006 (UTC)

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)
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 (talkcontribs) 20:27, 1 September 2006 (UTC)
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)

[edit] Syntax coloring

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 (talkcontribs) 16:20, 4 September 2006 (UTC)

I'll give it another couple of days, then remove it if no one replies. Supadawg (talkcontribs) 20:37, 6 September 2006 (UTC)
No objections, so I'll go ahead and remove the syntax coloring. Supadawg (talkcontribs) 00:48, 10 September 2006 (UTC)
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)

[edit] Language Wars

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)

[edit] Strategy for Algorithms with different outputs?

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.

For example, a QuickSort and a BubbleSort on the same simple list of data would produce the same ordered list, so according to the "same output" theory they'd be good candidates for the Strategy pattern. However, what if one wanted to (for example) create two algorithms that ordered objects by some complex ranking scheme, and the outputs of those two were different? As an example, let's say you could access the source for Google and Yahoo's search relevancy rankings in relation to a specific keyword and list of websites, and you made algorithms based on this to sort the given list of websites by relevancy for Google and Yahoo respectively. Would these two algorithms also be candidates for the Strategy pattern, or is there a more appropriate pattern to use?

Is there an expert watching this conversation who has an answer? It may be worth putting on the page if you do, because I'm sure there's others like me who came to this page hoping to find the answer to that (as well as alternative Design Patterns that would fit the bill if one of the scenarios described above don't actually fit into the intent of the Strategy pattern).

-Jack Colorado 75.39.171.37 01:07, 13 November 2007 (UTC)

It's still Strategy. Java's Comparator interface or C#'s IComparator, for instance, are interfaces for specifying an ordering strategy within a sort. Strategy is not just a way of specifying a private implementation detail in order to make trade offs in performance or whatever. It's a way to pass in a function to be used as piece of a larger algorithm. That's why the article says that first class functions are a replacement for this pattern.

Hmm, fascinating. Aye, this is good information, thanks for the post about it... it definitely clears things up for me. I think the kind of more explicit definition you gave here would be useful on the page. Saying that first class functions are a replacement is good if you know what a first class function is and how it fits in here, but I think it would be good to just outright say what you said above ("It's not just a way of specifying a private implementation detail..."). If you could elaborate on it a little more, I'd like to see a variant on your description on the page itself personally.

-Jack Colorado 24.10.88.246 (talk) 04:21, 18 November 2007 (UTC)

[edit] What is actionscript doing here?!

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