oop - Is 'Strategy Design Pattern' no more than the basic use of polymorphism? -


in strategy design pattern, is

  • create common interface.
  • implement set of classes using interface overridden method(s).
  • let run time choose actual class object has same type common interface , call overridden method(s) resolve correctly according class.

my question is, isn't basic example of polymorphism , method overriding learn?

other possibility of using abstract class too, replacing common interface.

what describe way implement strategy pattern. describe how implement whole lot of different designs, since there many, many reasons why might want create common interface, make different implementations, , select 1 @ runtime different situations.

there other ways implement strategy pattern.

but, know, design not code. design mental model of how software works -- human thing, not bits. design pattern common way of composing solutions common sorts of problems. again happens in head , not in bits.

the strategy pattern in particular making objects interchangeable algorithms, of used particular purpose.

so, yes, define interface... it's not interface data object -- doesn't have getters , setters , state mutators, etc. interface defines how object interacts algorithm used particular purpose. chess game might use "player" strategy, example, method called "selectnextmove".

and, yes make implementations of interface, implementations aren't "things" become "parts" of containing object, different algorithms used perform whatever function object requires. chess game support many different strategies used select next move computer player.

so when you're thinking design of chess game, example, in head, find useful think strategy choosing next move separately objects model board , ensure selected move recorded, communicated, , rendered properly. strategy choosing next move independent of these things.

if you're software designer, code independently too, separation of concerns in code mirror separation of concerns in head, make easy people maintaining code think in useful way, , allow new strategies choosing next move swapped in , out whenever want.