Collections.sort使用什么样的设计模式? [英] What's design pattern does Collections.sort use?

查看:181
本文介绍了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());
    }

});


推荐答案


以下列方式列出一个列表,什么是使用的设计模式?这里使用的技术是什么?

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 设计模式。这是一个简单的旧的多态。

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.

有些程序员可能称之为策略模式,但我不同意。 -a 关系。

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.

有些程序员可能会进一步争辩说,我们正在将 C c <排序方法,因此这是策略模式;然而,需要确认的是策略策略模式的组件之一。 策略模式的另一个重要组成部分是其上下文,与 策略。该组件对于策略模式背后的动机而言至关重要,它更适合继承 。你不能从整体上分离出来,仍然把这个分开的一部分称作一个整体。您不能从策略模式中获取上下文,而仍然调用策略模式的其余部分。

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.

支持资料

我们来看看策略模式的定义 GoF


在对象中封装算法是Strategy
(315)模式的意图。该模式的关键参与者是Strategy
对象(其封装不同的算法)以及他们操作的
中的上下文
。组合物是策略;它们封装了不同的格式化算法。 组合是合成策略的上下文

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..

现在应该清楚多态策略模式之间存在微妙的区别。 策略模式介绍使用组合上下文,如上面粗体所示。 集合类不与比较器建立组合关系。此外,策略模式类图显示组件称为构成策略界面的上下文

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模式的方法,那么我会比策略模式更紧密地说(不完全)类似于命令模式

This question was tagged as OOP but if you 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)

底线策略模式不能与多态或<功能编程。 Collections.sort 是一个简单的多态替换的例子,无论您是否使用功能编程或面向对象编程做这个替代还可以说,我们正在将策略排序到 sort 方法,但没有上下文,它不是策略模式的同义词。

Bottom line : The term Strategy Pattern is not interchangeable with Polymorphism or Functional Programming. 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. 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.

相关内容功能编程是否替换GoF设计模式?

这篇关于Collections.sort使用什么样的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆