Collections.sort 使用什么设计模式?

当以下列方式将比较器应用到列表时,所使用的设计模式或此处使用的技术是什么?

When applying a comparator to a list in the following manner, what is the design pattern being used or what is the technique being used here?

Collections.sort(myCollection, new Comparator<MyItem>() {

    @Override
    public int compare(MyItem item1, MyItem item2) {
        return item1.getId().compareTo(item2.getId());
    }

});

推荐答案

TL;DR :

Collections.sort 是一个简单的多态替换示例,无论您是使用函数式编程还是面向对象编程来进行这种替换.策略模式这个术语不能与多态性或函数式编程互换.

Collections.sort is an example of a simple polymorphic substitution regardless of whether you use Functional Programming or Object Oriented Programming to make this substitution. The term Strategy Pattern is not interchangeable with Polymorphism or Functional Programming.

我们仍然可以说我们将排序 Strategy 传递给 sort 方法,但没有 Context,它不等同于策略模式.

One could still say that we are passing a sorting Strategy to the sort method but without the Context, it is not synonymous to the Strategy Pattern.

当以下列方式将比较器应用到列表时,所使用的设计模式或此处使用的技术是什么?

When applying a comparator to a list in the following manner, what is the design pattern being used or what is the technique being used here?

由于此问题已被标记为 OOP,因此此处本身没有使用 OOP design-pattern.这是一个普通的旧多态.一些程序员可能将其称为策略模式,但我不同意.Strategy 模式提倡 Composition 而不是 Inheritiance,您使用 has-a 关系而不是 is-a 关系.

Since this question has been tagged as OOP, there is no OOP design-pattern being used here per-se. This is plain old Polymorphism in action. Some programmers may call this the Strategy Pattern but I disagree. The Strategy pattern advocates Composition over Inheritiance where you use a has-a relationship rather than an is-a relationship.

一些程序员可能会进一步争辩说,我们正在将排序 Strategy 传递给 Collections.sort 方法,所以这就是 Strategy Pattern;然而,需要承认的是,Strategy 是 Strategy Pattern 的组成部分之一.Strategy 模式的另一个重要组成部分是它的 Context,它与 Strategy 建立 HAS-A 关系.这个组件是 Strategy Pattern 背后动机的核心,它更喜欢 composition 而不是 inheritance.你不能从整体中取出一个部分,仍然把那个分离的部分称为一个整体.您不能将 Context 从 Strategy Pattern 中取出,而仍将其余部分称为 Strategy Pattern.

Some programmers may further argue that we are passing a sorting Strategy to the Collections.sort method so this is the Strategy Pattern; however, what one needs to acknowledge is that Strategy is one of the components of the Strategy Pattern. The other important component of the Strategy pattern is its Context that establish a HAS-A relationship with the Strategy. This component is central to the motivation behind the Strategy Pattern which is to prefer composition over inheritance. You can't take a part out of the whole and still call that separated part a whole. You can't take the Context out of the Strategy Pattern and still call the remainder the Strategy Pattern.

Collections.sort 是一个 static 方法,它允许您多态 将 Comparator 实现替换为在运行时使用.

Collections.sort is a static method that is allowing you to Polymorphically substitute the Comparator implementation to be used at runtime.

支持材料

让我们看看 策略模式的定义="nofollow noreferrer">GoF :

Let's take a look at the definition of Strategy Pattern from GoF :

将算法封装在对象中是 Strategy 的意图(315) 模式.该模式的主要参与者是策略对象(封装了不同的算法)和上下文他们经营的.合成器是策略;它们封装了不同的格式化算法.合成是合成策略的上下文.

Encapsulating an algorithm in an object is the intent of the Strategy ( 315) pattern. The key participants in the pattern are Strategy objects (which encapsulate different algorithms) and the context in which they operate. Compositors are strategies; they encapsulate different formatting algorithms. A composition is the context for a compositor strategy.

....

对象组合提供了一种可能更加可行和灵活的扩展机制..

Object composition offers a potentially more workable and flexible extension mechanism..

现在应该清楚多态性和策略模式之间的细微差别.Strategy 模式讨论了使用 composition 的 Context,如上面 bold 中突出显示的那样.Collections 类不与 Comparator 建立 composition 关系.此外,策略模式的类图显示了一个组件称为构成 Strategy 接口的 Context.

It should now be clear that there is a subtle difference between Polymorphism and the Strategy Pattern. The Strategy pattern talks about a Context that uses composition as highlighted in bold above. The Collections class does not establish a composition relationship with the Comparator. Furthermore, the class diagram for the Strategy Pattern shows a component called the Context which composes the Strategy interface.

这个问题被标记为 OOP 但如果我们想讨论 Collections.sort 在函数式编程范式中代表什么模式,我会说代表函数式编程.(如果我必须将函数传递给方法等同于 OOP 模式,我会说它更接近(不完全)类似于 Command 模式 而不是 Strategy 模式)

This question was tagged as OOP but if we want to talk about what pattern would Collections.sort represent when it comes to the functional programming paradigm, I would say it represents functional programming. (If I had to equate passing a function to a method to an OOP pattern, I would say it closely (not completely) resembles the Command Pattern more than the Strategy Pattern)

相关内容:函数式编程是否取代了GoF设计模式?

相关文章