Is the "strategy design pattern" nothing more than the main use of polymorphism?

In the Strategy Design Template , what we do is

  • Create a common interface.
  • Implement a set of classes using this interface with overridden methods.
  • Allow runtime to select the actual class for an object that is of the same type with this common interface, and will call the overridden method (s) that will be correctly resolved according to the class.

My question is: Is this not a basic example of polymorphism and redefinition of the method we are studying?

besides the possibility of using an abstract class, replacing the common interface.

+5
source share
7 answers

What you are describing is a way to implement a strategy template. You also describe how to implement many different designs, since there are many reasons why we might need to create a common interface, make different implementations, and choose one at runtime for different situations.

There are also other ways to implement a strategy template.

But, you know, design is not code. Design is a mental model of software work - a thing of man, not a bit. A design pattern is a general way to write solutions to common problems. Again this happens in your head, not in bits.

, , , .

, , ... - .. , , . "Player", , "SelectNextMove".

, , "", "" , , , . , .

, , , , , , , , , , . .

, , , , , , , , .

+5

, Javascript. , arround, , , .

:

Javascript sort, . , comparisonFunction, , 2 :

  • 1, .
  • 0,
  • -1, ,

, , :

[5, 2, 4, 1, 3].sort(); // default: sort in the natural order

[5, 2, 4, 1, 3].sort(function reverse(first, second){
    if (first > second) return -1;
    if (first == second) return 0;
    if (first < second) return 1;
});

[5, 2, 4, 1, 3].sort(function random(first, second){
    return Math.floor(Math.random() * 3) - 1;
});

, , , , .

+2

- :

, , , - , ( - " ", ). , Facade Pattern - .

, , " " - , , . , , .

+1

: , .

: , , . .

OO . . , .

+1

Strategy_pattern , .

. .

/ .

, . , , . , , .

UML:

enter image description here

:

+1

. , , .

, . . "" .

0

I would like to add that when using polymorphism you usually have to destroy the object and replace it with a new instance. Constantly creating / destroying a new instance can be expensive. A strategy template is one way to implement deferred loading / storage of previously created instances for future use.

0
source

Source: https://habr.com/ru/post/1664457/


All Articles