What is the difference between Factory and Strategy patterns?

前端 未结 12 1456
执念已碎
执念已碎 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:12

    Strategy and Factory are different purposes. In strategy you have the approach defined, using this pattern you can interchange the behavior (algorithms). Coming to Factory there are lot of variations around. But the original pattern from GO4 states factory leaves creation of object to child class. Here with the factory you are replacing complete instance not the behavior you are interested in. By this you will be replacing complete system not the algorithm.

    0 讨论(0)
  • 2020-12-02 05:15

    Strategy pattern in simple terms is more of runtime creation of behaviour where you are not concerned with the implementing class. On the other had factory is runtime creation of concrete class instance and it is up to you to use any behaviour(method) exposed by the implemented interface.

    0 讨论(0)
  • 2020-12-02 05:16

    The strategy pattern allows you to polymorphically change behavior of a class.

    The factory pattern allows you to encapsulate object creation.

    Gary makes a great point. If you are using the principle of coding to abstractions rather than "concretions" then a lot of the patterns start looking like variations on a theme.

    0 讨论(0)
  • 2020-12-02 05:17

    Factory (and FactoryMethod returned by Factory):

    1. Creational pattern
    2. Based on inheritance
    3. Factory returns a Factory Method (interface) which in turn returns Concrete Object
    4. You can substitute new Concrete Objects for interface and client (caller) should not be aware of all concrete implementations
    5. Client always access interface only and you can hide object creation details in Factory method

    Have a look at this wikipedia article and javarevisited article

    Strategy pattern:

    1. It's a behavioural pattern
    2. It's based on delegation
    3. It changes guts of the object by modifying method behaviour
    4. It's used to switch between family of algorithms
    5. It changes the behaviour of the object at run time

    Example:

    You can configure Discount strategy for a particular item ( AirFare ticket or ShoppingCart item). In this example, you will offer 25% discount to an item during July - December and No discount on the item during Jaunary - June.

    Related posts:

    Real World Example of the Strategy Pattern

    Design Patterns: Factory vs Factory method vs Abstract Factory

    0 讨论(0)
  • 2020-12-02 05:19

    A factory pattern is a creational pattern. A strategy pattern is an operational pattern. Put another way, a factory pattern is used to create objects of a specific type. A strategy pattern is use to perform an operation (or set of operations) in a particular manner. In the classic example, a factory might create different types of Animals: Dog, Cat, Tiger, while a strategy pattern would perform particular actions, for example, Move; using Run, Walk, or Lope strategies.

    In fact the two can be used together. For example, you may have a factory that creates your business objects. It may use different strategies based on the persistence medium. If your data is stored locally in XML it would use one strategy. If the data were remote in a different database, it would use another.

    0 讨论(0)
  • 2020-12-02 05:21

    Factory pattern is a creational pattern, which is created with specified properties(behaviour). while at run time after creation u cn't change it's properties(behaviour). so if u need different properties(behaviour) u have to delete the object and create new object with needed properties(behaviour). which is not gud. while in case of strategy pattern u can change the properties(behaviour) at run time.

    0 讨论(0)
提交回复
热议问题