What is the difference between Factory and Strategy patterns?

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

    I may digress with Oscar in that his example of a Factory implementation is rather tightly coupled and very closed, no wonder your pick is Strategy pattern. A Factory implementation should not depend on any fixed number of specific classes being instantiated, For example:

    public Command getCommand( int operatingSystem ) {        
       return commandTable.get(operatingSystem); 
    }
    
    ...
    
    public class WindowsCommand implements Command {
        ...
        static {
            CommandTable.getInstance().registerCommand(WIN_COMMAND_ID, new WindowsCommand());
        }
    
    }
    

    I guess the most appropriate criteria to choose one or another is mostly the terms you employ to name your classes and methods, taking into account we all should tend to program to interfaces and not to classes and also focus on the goal: we aim to determine which code will execute on runtime. That said, we can achieve the goal by using any of both patterns.

    0 讨论(0)
  • 2020-12-02 04:59

    Just to add to what tvanfosson said, a lot of the patterns look the same as far as implementation. That is, a lot have you create an interface where perhaps there wasn't one before in your code, and then create a bunch of implementations of that interface. The difference is in their purpose and how they are used.

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

    First of all a difference between simple factory and abstract factory must be made. The first one is a simple factory where you only have one class which acts as a factory for object creation, while in the latter you connect to an factory interface (which defines the method names) and then call the different factories that implement this interface which are supposed to have different implementations of the same method based on some criteria. For example, we have a ButtonCreationFactory interface, which is implemented by two factories, the first WindowsButtonCreationFactory (creates buttons with Windows look and feel) and the second LinuxButtonCreationFactory (creates buttons with Linux look and feel). So both these factories do have the same creation method with different implementations (algorithms). You can reference this in runtime based on the method that you type of button that you want.

    For example if you want buttons with Linux look and feel:

    ButtonCreationFactory myFactory = new LinuxButtonCreationFactory();
    Button button1 = myFactory.createButton(...);
    

    or if you want Windows buttons

    ButtonCreationFactory myFactory = new WindowsButtonCreationFactory();
    Button button1 = myFactory.createButton(...);
    

    Exactly in this case, it results in a kind of strategy pattern, since it differentiates algorithms for doing some creation. However, it differs from it semantically because it is used for OBJECT CREATION rather than operational algorithms. So, basically with abstract factory you have object creation using different strategies, which makes it very similar to the strategy pattern. However the AbstractFactory is creational, while the Strategy pattern is operational. Implementation wise, they result to be the same.

    0 讨论(0)
  • 2020-12-02 05:04
    • The Factory ( method ) Pattern.

    Create concrete instances only. Different arguments may result in different objects. It depends on the logic etc.

    • The Strategy Pattern.

    Encapsulate the algorithm ( steps ) to perform an action. So you can change the strategy and use another algorithm.

    While both look like very similar, the purpose is rather different, one purpose is to create the other is to perform an action.

    So. If your Factory method is fixed, you may have it like this:

     public Command getCommand( int operatingSystem ) { 
          switch( operatingSystem ) { 
               case UNIX    :
               case LINUX   : return new UnixCommand();
               case WINDOWS : return new WindowsCommand();
               case OSX     : return new OSXCommand();
           }
      }
    

    But suppose your factory needs more advanced or dynamic creation. You may add to the factory method an strategy and change it without having to recompile, the strategy may change at runtime.

    0 讨论(0)
  • 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

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

    To extend on what Oscar said and in reference to his code:

    The getCommand is the Factory and the UnixCommand, WindowsCommand and OSXCommand classes are Strategies

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