Re: Strategy or A factory with Template Method



"thinkmind" <ramesh.mandaleeka@xxxxxxxxx> wrote in message
news:1127822807.136760.66600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi Group
>
> I was going through some blogs on the net. Here is one I am looking at.
>
> What's wrong with the following code?
>
> void uploadFiles(Set<File> files, boolean sftp, boolean scp, ...) {
> if (sftp) {
> // upload via SFTP
> } else if (scp) {
> // upload via SCP
> } else {
> // upload via FTP
> }
> }
>
> What the author of the blog says to use Strategy Design Pattern to
> implement this.
>
> But my question is cannot we use a Factory Design Pattern with a
> Template Method. Which one is more appropriate. Can any one give me
> idea on this.
>
> Thanks & Regards
> Ramesh
>

I think I hit "send" too soon on the previous message.

What I was going to do was quote from an interesting site on patterns. The
quote is:

Strategy is like Template Method except in its granularity. [Coplien, C++
Report, Mar 96, p88]
Template Method uses inheritance to vary part of an algorithm. Strategy uses
delegation to vary the entire algorithm. [GOF, p330]

and the site is:
http://home.earthlink.net/~huston2/dp/patterns.html

Since the author of the snippet that you quoted was intending to replace the
entire algorithm with one from a child class, that would be Strategy. If
the author had specified the common parts of the algorithm and then intended
to delegate specific parts, that would be Template method.

Either way, the patterns are related very closely.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--


.



Relevant Pages

  • Re: Strategy or A factory with Template Method
    ... So Strategy is good than a Template Method as per GOF prefer ... Composition over inheritance and as Deligation is through Composition. ... >> What the author of the blog says to use Strategy Design Pattern to ... > Template Method uses inheritance to vary part of an algorithm. ...
    (comp.object)
  • Design Pattern Relationship Diagram - Design Patterns - Gang of Four
    ... but I am having trouble understanding the diagram on the ... inside back cover entitled "Design Pattern Relationships." ... An obvious one is the Factory Method (often used by the Template Method), ...
    (comp.lang.cpp)