策略设计模式和工厂方法设计模式
我开始学习设计模式。现在我明白了一些,但对我来说还有很多困惑。 策略DP和工厂方法DP有什么区别?对我来说,它们看起来都一样。
I start learning Design Patterns. Now I understand a little bit but there are quite a lot of confusions for me. What's the difference between Strategy DP and Factory Method DP? For me they both looks like the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
战略是关于行为的。工厂是关于创造/安装的。
假设您有一个算法来计算折扣百分比。您可以有该算法的 2 种实现;一种是针对常客,一种是针对特别好的客户。
您可以使用策略 DP 来实现此实现:创建一个接口和 2 个实现该接口的类。在一个类中,您实现常规折扣计算算法,在另一类中,您实现“好客户”算法。
然后,您可以使用工厂模式来实例化您想要的类。因此,工厂方法实例化常规客户折扣算法或其他实现。
简而言之:工厂方法实例化正确的类;策略实现包含必须执行的算法。
Strategy is about behavior. Factory is about creation/instatation.
Suppose you have an algorithm, to calculate a discount percentage. You can have 2 implementations of that algorithm; one for regular customers, and one for extra-ordinary good customers.
You can use a strategy DP for this implementation: you create an interface, and 2 classes that implement that interface. In one class, you implement the regular discount-calculation algorithm, in the other class you implement the 'good customers' algorithm.
Then, you can use a factory pattern to instantiate the class that you want. The factory method thus instantiates either the regular customer-discount algorithm, or the other implementation.
In short: the factory method instantiates the correct class; the strategy implementation contains the algorithm that must be executed.
策略将不同的行为封装在同一接口后面。您可以使用
new
运算符实例化策略。例如(与 Frederik 建议的业务案例相同):工厂方法封装了其他一些接口的实例化机制(可能是策略,但也可能是其他东西)。例如:
Strategy模式经常与Factory Method一起使用,而Factory Method则经常用于其他构造型的实例化,而不仅仅是Strategies。
Strategies incapsulate different behaviors behind the same interface. You instantiate Strategies with
new
operator. For example (the same business case as Frederik suggested):Factory Method incapsulates instantiation mechanism of some other interface (maybe a Strategy, but maybe something else). For example:
Strategy pattern is often used together with Factory Method, while Factory Method is often used for instantiation of other stereotypes, not only Strategies.
区别在于它们的意图:
工厂方法模式是一种创建模式,用于将对象实例化推迟到子类。另一方面,策略模式是一种行为模式,用于将算法与客户端代码解耦。
如果您需要通过定义返回特定类型实例的方法来抽象对象创建,但让子类实现它,则可以使用第一个。在 Java 中,示例如下:
请注意实际的对象实例化与 SomeAbstractClass 的实现有何不同。
另一方面,如果您需要将算法与调用代码解耦,则可以使用策略模式。这类似于MVC模式中View与Controller的通信方式。在假设 java MVC UI 工具包中,这可能如下所示:
现在,假设您有另一个组件,它不是单击响应滑动(即滑块),
请注意 Button 和 Slider 类(视图)中的逻辑如何执行一个动作是完全相同的(都遵循 ActionHandler)。因此,我们可以将它们拉到父类(Widget)并让子类只定义操作处理程序实现,如下所示:
通过使用策略模式,我们可以通过使用 ActionHandler 的另一个具体实现配置它们来更改小部件的行为。通过这种方式,小部件(视图)与操作处理逻辑(控制器)松散耦合。
我们可以通过将策略和工厂方法模式混合在一起来使事情变得更有趣,如下所示:
请注意,这是策略模式的说明性示例,而不是 Swing(Java 的默认 UI 工具包)的实现方式。
两种模式有些相似,因为它们将某些逻辑片段推迟到其他地方。这是设计模式中的一个常见主题,可以实现关注点分离。然而,延迟逻辑的性质或意图是完全不同的。工厂方法将创建逻辑推迟到子类(在我的示例中,创建具体的 ActionHandler 实例),而策略模式则执行算法(在我的示例中,当用户与特定组件交互时要做什么)。
The difference is in their intention:
The factory method pattern is a creational pattern used to defer object instantiation to subclasses. On the other end, strategy pattern is a behavioral pattern used to decouple an algorithm from client code.
You would use the first if you need to abstract object creation by defining a method that returns an instance of a specific type, but letting subclasses implement it. In Java, an example would be as follows:
Note how actual object instantiation is differed to the implementation by SomeAbstractClass.
On the other hand you would use the Strategy pattern if you need to decouple an algorithm from the calling code. This is similar to how the View communicates with the Controller in the MVC pattern. In a hypothetical java MVC UI kit this could be as follows:
Now, say you had another component that instead of clicking responded sliding (i.e. slider)
Note how in Button and Slider classes (Views) the logic of executing an action is exactly the same (both defer to the ActionHandler). We could therefore pull them to the parent class (Widget) and let subclasses just define the action handler implementation, like so:
By using the strategy pattern we can change our widget's behavior simply by configuring them with another concrete implementation of ActionHandler. In that way, widgets (views) are loosely coupled form action handling logic (controllers).
We could make things a little more interesting by mixing the strategy and the factory method pattern together, like so:
Please Note that this is an illustrative example of a strategy pattern and NOT how Swing (Java's default UI kit) is implemented.
Both patterns are somewhat similar in that they defer some piece of logic to somewhere else. This is a common theme in design patterns that enables the separation of concerns. However, the nature or intention of the deferred logic is completely different. Factory method defers creation logic to subclasses (in my example, the creation of concrete ActionHandler instances), whereas strategy pattern the execution of an algorithm (in my example, what to do when an the user interacts with a specific component).
您可以在 http://javabyranjith 找到策略模式的另一个 Java 实现。 blogspot.in/2017/04/strategy-design-pattern.html
和
工厂方法设计模式位于 http://javabyranjith.blogspot.in/2014/06/factory-method-design-pattern.html
You can find another java implementation for strategy pattern at http://javabyranjith.blogspot.in/2017/04/strategy-design-pattern.html
and
factory method design pattern at http://javabyranjith.blogspot.in/2014/06/factory-method-design-pattern.html