Design patterns 战略设计模式与工厂法设计模式
我开始学习设计模式。现在我懂了一点,但对我来说有很多困惑。策略DP和工厂方法DP之间有什么区别?对我来说,它们看起来都一样。策略是关于行为的。工厂是关于创建/安装的 假设您有一个算法,用于计算折扣百分比。该算法可以有2个实现;一个是普通客户,一个是特别普通的好客户。 您可以为此实现使用策略DP:创建一个接口和两个实现该接口的类。在一个类中,您实现常规折扣计算算法,在另一个类中,您实现“好客户”算法 然后,您可以使用工厂模式来实例化所需的类。因此,工厂方法实例化常规客户折扣算法或其他实现Design patterns 战略设计模式与工厂法设计模式,design-patterns,Design Patterns,我开始学习设计模式。现在我懂了一点,但对我来说有很多困惑。策略DP和工厂方法DP之间有什么区别?对我来说,它们看起来都一样。策略是关于行为的。工厂是关于创建/安装的 假设您有一个算法,用于计算折扣百分比。该算法可以有2个实现;一个是普通客户,一个是特别普通的好客户。 您可以为此实现使用策略DP:创建一个接口和两个实现该接口的类。在一个类中,您实现常规折扣计算算法,在另一个类中,您实现“好客户”算法 然后,您可以使用工厂模式来实例化所需的类。因此,工厂方法实例化常规客户折扣算法或其他实现 简而言之
简而言之:工厂方法实例化了正确的类;策略实现包含必须执行的算法。策略在同一接口后面包含不同的行为。您可以使用新操作符实例化策略。例如,Frederik建议的相同商业案例:
DiscountCalculator calc = new GoogCustomerDiscountCalculator();
Integer discount = calc.calculate();
工厂方法包含了一些其他接口的实例化机制,可能是一种策略,但也可能是其他的。例如:
DiscountFactory factory = new DiscountFactory();
DiscountCalculator calc = factory.getDiscountCalculator();
Integer discount = calc.calculate();
策略模式通常与工厂方法一起使用,而工厂方法通常用于实例化其他原型,而不仅仅是策略。不同之处在于它们的意图: 工厂方法模式是一种创造性模式,用于将对象实例化延迟到子类。另一方面,策略模式是用于将算法与客户端代码解耦的行为模式 如果需要通过定义一个返回特定类型实例的方法来抽象对象创建,但让子类实现它,则可以使用第一种方法。在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模式中视图与控制器的通信方式。在假设的java MVC UI工具包中,这可能如下所示:
// 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完全相同。因此,我们可以将它们拉到父类小部件,让子类定义动作处理程序实现,如下所示:
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());
}
}
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();
}
}
通过使用strategy模式,我们可以通过配置ActionHandler的另一个具体实现来简单地改变小部件的行为。这样,小部件视图与操作处理逻辑控制器松散耦合
通过将策略和工厂方法模式混合在一起,我们可以使事情变得更加有趣,如下所示:
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());
}
}
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();
}
}
请注意,这是一个策略模式的示例,而不是SwingJava默认UI工具包的实现方式
这两种模式有些相似,因为它们将某些逻辑延迟到其他地方。这是设计模式中的一个常见主题,它支持关注点的分离。然而,延迟逻辑的性质或意图是完全不同的。工厂方法将创建逻辑延迟到我的示例中的子类,即创建具体的ActionHandler实例,而策略模式在我的示例中是执行算法,当用户与特定组件交互时该怎么做。您可以在 及
工厂方法设计模式在这是两种非常不同的模式。是什么让你觉得他们是一样的?你能说出一些相似之处吗?@space>>我不知道。但对我来说,他们都试图实现同样的目标。我是DP的新手!你能解释一下吗,这对其他人也很有用!谢谢我的评论意在成为一个鼓舞人心的问题。有时候,在这样的情况下,写下模式之间的相似性和差异是有帮助的。这有时可以帮助您自己找到解决方案。@Frederik但为什么我不能在策略模式中实现工厂?不同的算法是创建对象的不同方式。模式描述了如何解决常见问题。Strategy描述了如何解决不同的计算方法。工厂描述如何实例化某个接口的不同实现;工厂并不意味着定义创建同一对象的不同方式。我允许您抽象出
必须为某个接口实例化。给出的示例不是工厂方法。GoF工厂模式如下所述: