Jump to content


Photo
- - - - -

Strategy Pattern Example


  • Please log in to reply
10 replies to this topic

#1 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 28 April 2011 - 11:27 PM

*
POPULAR

I have written a paper on the Strategy Pattern (with appendices on Interfaces and the Factory Method Pattern) in the hope that this will be a useful guide to others.

We have published a PDF version of the paper:
StrategyPatternWithLabVIEW

and a zip file that also includes the source and model:
StrategyPatternWithLabVIEW--WithModelAndSource.

You can comment on the paper here, if you like.

Paul

#2 QFang

QFang

    4 more posts to go.

  • Members
  • 6 posts
  • Version:LabVIEW 2011
  • Since:2005

Posted 26 April 2012 - 03:00 PM

Thank you for making and sharing this. I found the explanations very helpful. My big complaint right now is that I'm incredibly hungry and its only 11am...

Thanks!!
Q

#3 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 26 April 2012 - 03:46 PM

I'm quite happy to hear it was helpful! Thanks for the feedback!

#4 Aristos Queue

Aristos Queue

    LV R&D: I write C++/# so you don't have to.

  • Premium Member
  • 2,620 posts
  • Location:Austin, TX
  • Version:LabVIEW 2011
  • Since:2000

Posted 27 April 2012 - 04:19 AM

My two questions:
1) I have not yet read the paper... is the implementation you give one that you have actually deployed successfully in at least one real application? There have been declarations of "this is a pattern" from folks that have never actually tested their implementation under real-world scenarios and those declarations have turned out false.
2) Assuming it is valid (which I'm expecting given your history of quality), do you mind if we link to it from the patterns page on ni.com?

#5 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 27 April 2012 - 05:09 PM

1) Yes, although we use the State Pattern -- implemented in exactly the same way -- more often -- in every component, actually, and it is not too much to say that the latter has changed our lives. :) The pattern itself is a GoF pattern and is the focus of the first chapter in Head First Design Patterns (http://www.amazon.co...35546001&sr=1-1).
2) Of course you may. It is already linked here, actually (https://decibel.ni.c.../docs/DOC-13311), albeit as an add-on.

I hope you do read the paper. I think you will like it.

Paul

#6 Aristos Queue

Aristos Queue

    LV R&D: I write C++/# so you don't have to.

  • Premium Member
  • 2,620 posts
  • Location:Austin, TX
  • Version:LabVIEW 2011
  • Since:2000

Posted 27 April 2012 - 10:45 PM

I did finish reading the paper. Good writeup. I have only one request: At several points in the document, you use the term "delegate". I would like you to add comments to specifically disambiguate this pattern from the delegate pattern because the two are nearly identical in implementation. I *think* the major difference between the two is whether the secondary object is supplied from the outside or whether the primary object chooses the secondary itself. It is a fine line distinction that I am not clear on, and I think it is worth touching on.

#7 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 27 April 2012 - 11:52 PM

Thanks for the feedback. I'll see what I can find. It is worth noting that the GoF book uses the term when discussing the Strategy Pattern (p. 318).

#8 CRoebuck

CRoebuck

    More Active

  • Premium Member
  • 41 posts
  • Location:Portsmouth, England
  • Version:LabVIEW 2012
  • Since:1994

Posted 28 April 2012 - 08:45 AM

Really good job. I have read several whitepapers written by you in the past and like your style. I'm off to dig aorund the example code so may be back with questions as I'm a hopeless in the kitchen.

#9 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 01 May 2012 - 06:33 PM

Thanks! I look forward to your questions!

#10 PaulL

PaulL

    Extremely Active

  • Premium Member
  • 462 posts
  • Location:Flagstaff AZ
  • Version:LabVIEW 2011
  • Since:1997

Posted 08 June 2012 - 10:27 PM

Hmm... "Delegation Pattern" is not one of the GoF patterns. There is a Wikipedia article (http://en.wikipedia....egation_pattern) that gives us simple example without an interface and then many examples with an interface.
This article (http://best-practice...delegation.html) gives similar examples, and mentions that "Several other design patterns use delegation - the State, Strategy and Visitor Patterns depend on it." It looks to me like the key idea is that a class assigns a task to some other class that it possesses through Composition. The type of the attribute may actually be abstract so we can vary the specific type. Anyway, that's after a brief read. I haven't looked at this before.

#11 Daklu

Daklu

    Bringing the Fu to you

  • Premium Member
  • 1,752 posts
  • Location:Seattle
  • Version:LabVIEW 2009
  • Since:2006

Posted 13 June 2012 - 07:45 PM

Hmm... "Delegation Pattern" is not one of the GoF patterns.

I know the "Delegation Pattern" has been mentioned in some early versions of OO pattern documentation on NI's website. In my opinion it is more of a strategy or technique than a pattern. Delegation just means having some other class or component do the work. Patterns give class structures (sometimes with minor variations) to solve fairly specific problems. Delegation is far too broad a topic to be contained in a pattern.

The Best Practices link illustrates the problem. Compare the "Single Inheritance with Delegation" diagram with the "Cat" diagram in the structure section. The class structures are substantially different and there is no explanation as to why they are different. The Cat diagram is an example of dependency injection. (I do this all the time and it is hugely beneficial for unit testing.) I'm not quite sure what to make of the "Single Inheritance..." example. It doesn't work as a general replacement to multiple inheritance like the text implies, though it might be adequate for specific situations. Yes, both examples use delegation to achieve their results, but neither adequately defines delegation.


It looks to me like the key idea is that a class assigns a task to some other class that it possesses through Composition.

This is really the comment that prompted me to respond before I got sidetracked on the response above. Delegating to a composed class is probably the most common way it is used, but I don't think it is necessarily a "key idea" of delegation. There are lots of ways to ask another class to perform a task without using a direct method call.

Certified LabVIEW Architect
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.

Yes, the QSM is flexible. So is Jello. That doesn't make it good construction material.

There are two secrets to success:
Secret #1 - Never tell everything you know.