策略设计模式和抽象工厂模式有什么区别?

发布于 2024-10-16 01:55:36 字数 54 浏览 6 评论 0原文

有人可以一劳永逸地向我解释这两者之间的区别,并尝试为何时使用它们提供一种指导吗?例子真的很好。

Can someone once and for all explain to me the difference between these two and try to give a sort of guideline for when to use each one of them? Examples would be really nice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-10-23 01:55:36

除了结构和实现细节之外,意图有所不同。一旦你掌握了意图的重要性这一基本理念,那么你就会走在正确的道路上。

了解意图在设计模式中的作用

战略意图。这是一种行为模式

  1. 定义一系列算法,封装每个算法,并使它们可以互换。策略使算法独立于使用它的客户端而变化。
  2. 捕获接口中的抽象,将实现细节隐藏在派生类中。

抽象工厂的意图。这是一种创建模式,

  1. 提供一个用于创建相关或依赖对象系列的接口,而无需指定它们的具体类。
  2. 封装的层次结构:许多可能的“平台”,以及一套“产品”的构建。

The INTENT is different other than structural and implementation details. As soon as you grasp this fundamental idea that INTENT is of significance, then you will be on the right path.

Understand the role of intent in design patterns

Intent for Strategy. This is a Behavioral Pattern

  1. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
  2. Capture the abstraction in an interface, bury implementation details in derived classes.

Intent for Abstract Factory. This is a Creational Pattern

  1. Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  2. A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
美人迟暮 2024-10-23 01:55:36

策略是针对没有一流函数的语言的一种解决方法。您传入一个策略对象,该对象决定您希望与其余代码分开的某些策略。想想 Java 中的排序以及它们如何使用比较器,比较器是一个策略对象,它允许您独立于排序算法指定排序策略。这允许您通过加入不同的策略来重用代码。

抽象工厂是一个用于创建其他对象的对象,抽象部分是您有一个返回工厂实现的工厂,工厂的用户通过接口访问它。因此,可以将一种工厂实现替换为另一种工厂实现,而无需更改工厂的用户,因为这些用户仅依赖于对象的接口。

Strategy is a workaround for languages that don't have first-class functions. You pass in a strategy object that decides some policy that you want separated from the rest of the code. Think of sorting in Java and how they use Comparators, a Comparator is a strategy object that allows you to specify the policy for sorting separately from the sorting algorithm. That allows you to reuse the code by dropping in different strategies.

Abstract Factory is an object used to create other objects, with the abstract part being that you have a factory that returns an implementation of the factory, where users of the factory access it through an interface. So one factory implementation can be swapped out for another with no changes to users of the factories, because those users are depending on the objects' interfaces only.

简单 2024-10-23 01:55:36

工厂设计模式充当工厂,并根据您的请求在运行时生成不同类型的对象。因此,控制是在运行时进行的,您可以在运行时决定所需的对象。这样对象之间的耦合就会减少。

另一方面,策略设计模式允许您通过使用“有一个”关系而不是“是一个”关系来更好地控制层次结构。因为如果我们试图在更复杂的场景中使用继承 (is a),那么我们可能必须重写几代继承的方法,从而避免代码重用。

假设您创建了一个 Animal 类(或接口)并且其中有一个 move 方法。

您希望通过扩展它来创建一个鸟类类。因此,您将在 move() 方法下添加飞行功能。但企鹅的情况又如何呢?他们将具有步行功能。

因此,每次通过扩展 Animal 类创建动物时,您都必须一次又一次地重写 move() 方法。您可以清楚地了解 move() 方法更容易受到更改,因此最佳实践是将更改部分与核心代码分开。

我们可以简单地为 move() 方法创建一个单独的层次结构。您可以通过 setter 方法将相关的 move() 方法分配给对象。因此控制权被传递给运行时。下图说明了这一事实;

在此处输入图像描述

以下是我的 2 篇博客文章,如果您愿意,可以参考它们以获取更多详细信息。

  1. 工厂设计模式

  2. 策略设计模式

Factory Design pattern act as a factory and generates different kinds of objects at run time as per your request. So control is at run time and you get to decide the object you require at run time. So coupling among objects will be reduced.

On the other hand, Strategy design pattern allows you to take better control of hierarchical structures by using "has a" relationship over "is a" relationship. Because if we tempt to use inheritance (is a) in a more complex scenario then we may have to override the methods inherited over generations and it avoids code reuse.

Let's say you create an Animal class(or interface) and you are having a move method in it.

And you are hoping to create a bird class by extending it. So you will add flying features under move() method. But what is the case with penguins? They will have walking features.

So every time you create an animal by extending the Animal class, you will have to override the move() method again and again. You can clearly understand move() method is more vulnerable to changes, so best practice is to separate the changing part from core codes.

Simply we can create a separate hierarchy to move() methods. You can assign related move() method to the object via setter methods. So control is passed to run time. Following diagram illustrates that fact;

enter image description here

Following are 2 of my blog posts if you like you can refer them for further details.

  1. Factory Design pattern

  2. Strategy Design pattern

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文