Stránka 1 z 1

Rozdíl mezi Command a Strategy pattern

Napsal: 27 kvě 2018 00:20
od bohmacek
Zdravím,

začal jsem se zaobírat návrhovými vzory, a nechápu rozdíl mezi Strategy pattern a Command pattern. Je tu někdo takový, kdo mi mohl tento rozdíl objasnit ?
Děkuji

Re: Rozdíl mezi Command a Strategy pattern

Napsal: 27 kvě 2018 02:03
od lubo007
do design patternov teda moc nevidim, ale:

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.[1]

Re: Rozdíl mezi Command a Strategy pattern

Napsal: 27 kvě 2018 09:13
od faraon
Obojí slouží k tomu, aby levné kódovací opice co nejrychleji nabušily co největší množství kódu, který se okamžitě začne prodávat, a managoři si pořídili další Ferrari.

Místo těchle sr[PÍP]ek se radši nauč programovat. Studuj Knutha a poznej moudrost patriarchů.

Například:

Kód: Vybrat vše

The Evolution of a Programmer



 High School/Jr.High
 ===================
        10 PRINT "HELLO WORLD"
        20 END



 First year in College
 =====================
         program Hello(input, output)
          begin
             writeln('Hello World')
          end.



 Senior year in College
 ======================
         (defun hello
          (print
           (cons 'Hello (list 'World))))



 New professional
 ================
         #include
        void main(void)
         {
          char *message[] = {"Hello ", "World"};
           int i;

           for(i = 0; i < 2; ++i)
             printf("%s", message[i]);
           printf("\n");
        }



 Seasoned professional
 =====================
        #include
        #include
         class string
        {
         private:
          int size;
           char *ptr;
         public:
           string() : size(0), ptr(new char('\0')) {}
           string(const string &s) : size(s.size)
          {
           ptr = new char[size + 1];
           strcpy(ptr, s.ptr);
           }
           ~string()
          {
           delete [] ptr;
           }
           friend ostream &operator <<(ostream &, const string &);
           string &operator=(const char *);
          };
         ostream &operator<<(ostream &stream, const string &s)
         {
           return(stream << s.ptr);
         }
         string &string::operator=(const char *chrs)
         {
          if (this != &chrs)
           {
            delete [] ptr;
          size = strlen(chrs);
          ptr = new char[size + 1];
             strcpy(ptr, chrs);
          }
           return(*this);
        }
         int main()
         {
           string str;
           str = "Hello World";
           cout << str << endl;
           return(0);
         }



 Apprentice Hacker
 ===================
 #!/usr/local/bin/perl
 $msg="Hello, world.\n";
 if ($#ARGV >= 0) {
   while(defined($arg=shift(@ARGV))) {
     $outfilename = $arg;
     open(FILE, ">" . $outfilename) || die "Can't write $arg: $!\n";
     print (FILE $msg);
     close(FILE) || die "Can't close $arg: $!\n";
   }
 } else {
   print ($msg);
 }
 1;



 Experienced Hacker
 ===================
 #include
 #define S "Hello, World\n"
 main(){exit(printf(S) == strlen(S) ? 0 : 1);}



 Seasoned Hacker
 ===================
 % cc -o a.out ~/src/misc/hw/hw.c
 % a.out



 Guru Hacker
 ===================
 % cat
 Hello, world.
 ^D



 New Manager
 ===================
 10 PRINT "HELLO WORLD"
 20 END



 Middle Manager
 ===================
 mail -s "Hello, world." bob@b12
 Bob, could you please write me a program that prints "Hello,
world."?
 I need it by tomorrow.
 ^D



 Senior Manager
 ===================
 % zmail jim
 I need a "Hello, world." program by this afternoon.



 Chief Executive
 ===================
 % letter
 letter: Command not found.
 % mail
 To: ^X ^F ^C
 % help mail
 help: Command not found.
 % damn!
 !: Event unrecognized
 % logout

Re: Rozdíl mezi Command a Strategy pattern

Napsal: 28 kvě 2018 23:47
od CZechBoY
Command je jen taková dejme tomu přepravka na data, prostě zadáš nějakej příkaz a ten se vykoná. Na Wikipedii je příklad příkazu vypni světlo, zapni světlo, ...
https://en.wikipedia.org/wiki/Command_pattern#C#

Strategy ale přímo určuje jak se s danými daty pracovat. Na Wikipedii je příklad strategie normální zákazník (běžná cena)
a vip zákazník/happy hour (nižší cena).
https://en.wikipedia.org/wiki/Strategy_pattern#C#

Pokud bys chtěl oba vzory zamíchat do sebe tak ti může vyjít např. příkaz "vypnutí světla" strategií "co nejrychleji"/"skákej po jedné noze".