Polymorphism vs Strategy pattern

后端 未结 10 933
陌清茗
陌清茗 2021-01-30 10:12

What is the difference between the Strategy pattern and Polymorphism in Java?

I\'m confused that whatever is achieved via Strategy Pattern is

10条回答
  •  悲哀的现实
    2021-01-30 11:01

    Polymorphism is a principle and Strategy is a design pattern

    From oracle documentation page

    The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

    Polymorphism can be achieved in compile time ( method overloading) and run time (method overriding).

    Strategy_pattern

    1. Defines a family of algorithms,
    2. Encapsulates each algorithm, and
    3. Makes the algorithms interchangeable within that family.

    Strategy can use run-time polymorphism principle to achieve the desired functionality.

    Strategy pattern had one more component called Context in it's URL diagram. Refer to below SE posts:

    Real World Example of the Strategy Pattern

    Does this Java Strategy pattern have a redundant Context class?

    Few more useful articles:

    strategy by sourcemaking

提交回复
热议问题