我开始学习设计模式。现在我有点理解了,但是我有很多困惑。策略DP和Factory方法DP有什么区别?对我来说他们看起来都是一样的。
发布于 2011-03-21 08:16:03
策略是关于行为。工厂是关于创造/创造的。
假设您有一个计算折扣百分比的算法。您可以使用该算法的两个实现;一个用于常规客户,另一个用于非常普通的优秀客户。
您可以对此实现使用策略DP :创建一个接口,以及实现该接口的2个类。在一个类中,您实现了常规折扣计算算法,而在另一个类中,您实现了“好客户”算法。
然后,可以使用工厂模式来实例化所需的类。因此,工厂方法实例化了常规的客户折扣算法或其他实现。
简而言之:工厂方法实例化了正确的类;策略实现包含必须执行的算法。
发布于 2011-03-21 09:37:06
策略封装在同一个接口背后的不同行为。使用new运算符实例化策略。例如(与Frederik建议的相同的业务案例):
DiscountCalculator calc = new GoogCustomerDiscountCalculator();
Integer discount = calc.calculate();工厂方法封装了其他接口的实例化机制(可能是策略,但可能是其他接口)。例如:
DiscountFactory factory = new DiscountFactory();
DiscountCalculator calc = factory.getDiscountCalculator();
Integer discount = calc.calculate();策略模式通常与Factory方法一起使用,而Factory方法通常用于实例化其他构造型,而不仅仅是策略。
发布于 2012-09-04 14:48:10
不同之处在于他们的意图:
工厂方法模式是一个创建模式,用于将对象实例化推迟到子类。另一方面,策略模式是一种用于将算法与客户端代码解耦的行为模式。
如果需要通过定义返回特定类型实例的方法来抽象对象创建,则可以使用第一种方法,但让子类实现它。在Java中,一个例子如下:
public interface SomeAbstractObject {
// methods...
}
public abstract class SomeAbstractClass {
public abstract SomeAbstractObject newSomeObject();
// Other methods...
}
public class SomeConcreteClassA extends SomeAbstractClass {
public SomeAbstractObject newSomeObject() {
// assuming SomeConcreteObjectA extends from SomeAbstractObject
return new SomeConcreteObjectA();
}
// Other methods...
}
public class SomeConcreteClassB extends SomeAbstractClass {
public SomeAbstractObject newSomeObject() {
// assuming SomeConcreteObjectB extends form SomeAbstractObject
return new SomeConcreteObjectB();
}
// Other methods...
}注意实际的对象实例化与SomeAbstractClass的实现是如何不同的。
另一方面,如果需要将算法与调用代码解耦,则可以使用策略模式。这类似于视图如何在MVC模式中与Controller通信。在假设的 java工具包中,可以如下所示:
// interface abstracting the algorithm of a user interaction with your ui components.
public interface ActionHandler {
public void handle(Action a);
}
// concrete implementation of button clicked algorithm.
public class ButtonClickedHandler implements ActionHandler {
public void handle(Action a) {
// do some fancy stuff...
}
}
public class Button extends Widget {
// ActionHandler abstracts the algorithm of performing an action.
private ActionHandler handler = new ButtonClickedHandler();
// Delegates to the action handler to perform the action.
public void execute(Action a) {
handler.handle(a);
}
}现在,假设您有另一个组件,而不是单击响应滑动(即滑块)
public class Slider extends Widget {
// SliderMovedHandler extends ActionHandler
private ActionHandler handler = new SliderMovedHandler()
// Delegates to action handler to perform the action.
public void execute(Action a) {
handler.handle(a);
}
}注意,在Button和Slider类(视图)中,执行操作的逻辑是完全相同的(两者都服从于ActionHandler)。因此,我们可以将它们拉到父类(Widget),然后让子类只定义操作处理程序实现,如下所示:
public class Widget {
private ActionHandler handler;
public Widget(ActionHandler handler) {
this.handler = handler;
}
public void execute(Action a) {
handler.handle(a);
}
}
// concrete widget implementations change their behavior by configuring
// different action handling strategies.
public class Button extends Widget {
public Button() {
super(new ButtonClickedHandler());
}
}
public class Slider extends Widget {
public Slider() {
super(new SliderMovedHandler());
}
}通过使用策略模式,我们可以通过使用ActionHandler的另一个具体实现来简单地配置小部件的行为。这样,小部件(视图)是松散耦合的动作处理逻辑(控制器)。
通过将策略和工厂方法模式混合在一起,我们可以使事情变得更有趣,如下所示:
public abstract class Widget {
public void execute(Action a) {
// action handling strategy is retrieved by a factory method
ActionHandler handler = getActionHandler();
handler.handle(a);
}
// factory method defers creation of action handling strategy to subclasses
public abstract ActionHandler getActionHandler();
}
// factory method defines different action handling strategy for different subclass
public class Button extends Widget {
public ActionHandler getActionHandler() {
return new ButtonClickedHandler();
}
}
public class Slider extends Widget {
public ActionHandler getActionHandler() {
return new SliderMovedHandler();
}
}请注意,这是一个策略模式的示例,而不是Swing (Java的默认UI工具包)是如何实现的。
这两种模式有点相似,因为它们将一些逻辑推迟到其他地方。这是设计模式中的一个共同主题,可以实现关注点的分离。然而,延迟逻辑的性质或意图是完全不同的。工厂方法将创建逻辑推迟到子类(在我的示例中,创建具体的ActionHandler实例),而策略模式则是算法的执行(在我的示例中,用户与特定组件交互时应该做什么)。
https://stackoverflow.com/questions/5375187
复制相似问题