有人能解释一下工厂模式和战略模式之间的区别吗?

对于我来说,两者看起来是一样的,除了一个额外的工厂类(在工厂模式中创建一个product对象)


当前回答

我可能会偏离Oscar的主题,因为他的工厂实现的例子是紧密耦合且非常封闭的,难怪您选择了策略模式。工厂实现不应该依赖于任何固定数量的被实例化的特定类,例如:

public Command getCommand( int operatingSystem ) {        
   return commandTable.get(operatingSystem); 
}

...

public class WindowsCommand implements Command {
    ...
    static {
        CommandTable.getInstance().registerCommand(WIN_COMMAND_ID, new WindowsCommand());
    }

}

我想选择一个或另一个最合适的标准主要是用于命名类和方法的术语,考虑到我们都应该倾向于针对接口而不是类编程,并关注目标:我们的目标是确定哪些代码将在运行时执行。也就是说,我们可以通过使用这两种模式中的任何一种来实现目标。

其他回答

工厂模式是一种创建模式。战略模式是一种操作模式。换句话说,工厂模式用于创建特定类型的对象。策略模式用于以特定方式执行一个操作(或一组操作)。在经典的例子中,工厂可能会创建不同类型的动物:狗、猫、虎,而策略模式将执行特定的动作,例如,移动;使用跑,走或Lope策略。

事实上,这两者可以一起使用。例如,您可能有一个创建业务对象的工厂。它可以根据持久性介质使用不同的策略。如果数据以XML形式存储在本地,则使用一种策略。如果数据位于不同的远程数据库中,则它将使用另一个数据库。

我可能会偏离Oscar的主题,因为他的工厂实现的例子是紧密耦合且非常封闭的,难怪您选择了策略模式。工厂实现不应该依赖于任何固定数量的被实例化的特定类,例如:

public Command getCommand( int operatingSystem ) {        
   return commandTable.get(operatingSystem); 
}

...

public class WindowsCommand implements Command {
    ...
    static {
        CommandTable.getInstance().registerCommand(WIN_COMMAND_ID, new WindowsCommand());
    }

}

我想选择一个或另一个最合适的标准主要是用于命名类和方法的术语,考虑到我们都应该倾向于针对接口而不是类编程,并关注目标:我们的目标是确定哪些代码将在运行时执行。也就是说,我们可以通过使用这两种模式中的任何一种来实现目标。

工厂(方法)模式。

只创建具体实例。不同的参数可能导致不同的对象。这取决于逻辑等等。

战略模式。

封装算法(步骤)以执行操作。所以你可以改变策略,使用另一种算法。

虽然两者看起来非常相似,但目的却截然不同,一个目的是创造,另一个目的是执行动作。

所以。如果你的Factory方法是固定的,你可以像这样:

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

但是假设您的工厂需要更高级或更动态的创建。你可以在工厂方法中添加策略并在不需要重新编译的情况下更改它,策略可以在运行时更改。

工厂模式和策略模式之间的关键区别是在哪里进行操作。工厂模式对创建的对象执行操作(工厂类在创建后完成工作),而策略模式对上下文类本身执行操作。

若要将工厂模式更改为策略模式,则不从工厂类返回创建的对象,将对象保存在上下文类中,并在上下文类中创建包装器方法来执行操作,而不是直接从创建的对象执行操作。

虽然有人可能会问我们是否可以对创建的对象进行操作,但为什么我们仍然需要在上下文类中创建包装器呢?好的,关键是操作。策略模式可以根据策略改变操作,而且不需要改变对象,可以依靠上下文对象来做不同的操作,而不需要改变对象本身。

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.

例如,如果你想要带有Linux外观和感觉的按钮:

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

或者你想要Windows按钮

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.