What is the difference between Factory and Strategy patterns?

前端 未结 12 1486
执念已碎
执念已碎 2020-12-02 04:53

Can any one explain the difference between factory and strategy patterns?

For me both are looking same other than an extra factory class (which create an object of

12条回答
  •  抹茶落季
    2020-12-02 05:05

    You cannot understand the difference simply by looking at the code or categorization. To grasp the GoF patterns correctly, look for their intents:

    Strategy: "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."

    Factory Method: "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."

    And here's an elaborate explanation about the intents and the differences between these two patterns: Difference between Factory Method and Strategy design patterns

提交回复
热议问题