过滤项目集合的设计模式? [英] Design pattern for filtering a collection of items?

查看:137
本文介绍了过滤项目集合的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下典型的应用程序类型,其中包含具有不同属性的项目列表。例如。具有100个项目的树视图,每个项目具有名称评级 / em>之间或者在之间也存在多对多关系。 项目创建者等等。

Imagine the typical type of application where you have a list of items with different properties. E.g. a tree-view with 100 items, each having a name, a rating, a rank-within-the-hottest-items-on-the-planet etc. Probably there are also many-to-many relationships between items and item-catalogs, or between items and item-creators etc etc.

现在这个应用程序自然需要一个过滤系统。例如。我可以在不同关系的数据之间构造具有多种条件的复杂过滤器。

Now this application naturally needs a filtering system. E.g. where I can construct complex filters with multiple conditions of all kinds, between data in different relationships.

编写这样一个过滤特性的设计任务应该是许多开发人员完成,肯定必须有某种最适合任务的设计模式。

The design task of writing such a filtering feature ought to be something MANY developers have done and there surely must be some kind of design pattern which is the most suitable for the task.

任何人?

修改:切换到社区Wiki,因为我怀疑没有任何行业de因素模式。

Switched to community wiki since I suspect there isn't any industry de factor pattern used for this. Question too generally formulated I guess.

推荐答案

这是一个有点难以真正指出你想要什么,所以我会带我自己的假设。

It's a bit difficult to actually point out what you want, so I'll take my own assumptions.


  1. 过滤后基本集合应保持不变

  2. 结果不持久

一种典型的方法是使用观看次数。这基本上是惰性编程,您可以在其中创建一个对象,该对象可以访问原始集合,并知道要应用的过滤器,但只要不需要就不进行任何计算。

A classic approach is the use of views. This is basically lazy programming, where you create an object which can access the original collection and knows the filter to apply but will not make any computation as long as nothing is required.

在集合上,视图通常用迭代器实现,对于过滤,当然是已经指出的策略模式。

On collections, the views are often implemented with iterators, and for the filtering, of course a Strategy Pattern as already pointed out.

例如:

Collection myCollection;
Predicate myFilter;

// Nothing is computed here
View<Predicate> myView(myCollection, myFilter);

// We iterate until we find the first item in the collection that satisfies
// the Predicate, and no more, to initialize `begin`
View<Predicate>::Iterator begin = myView.begin(), end = myView.end();

净优点是,如果你只需要10个第一项,只应用谓词尽可能多的找到那些10首先,没有更多。

The net advantage is that if you (say) only need the 10 first items, then you'll only apply the predicate as much as necessary to find those 10 first, and no more.

此外,没有涉及的元素的副本,并且您的视图被保证即使您修改 myCollection ,也会更新,尽管这可能会影响迭代器的有效性(像往常一样)。

Also, there is no copy of the elements involved, and your view is guaranteed to be updated even if you modify myCollection, although this may affect the validity of the iterators (as usual).

问题是,(除非你实现缓存),每次计算结果。

The problem is that (unless you implement caching), the result is computed each time.

如果你想要一个更持久的结果,那么你最好构建一个新的集合只包含过滤的项(或对它们的引用)。这里没有一般的模式,这取决于你想使用'过滤'列表。

If you want a more persistent result, then you'd better build a new collection containing only the filtered items (or references to them). There is no general pattern here for it depends on how you want to use the 'filtered' list.

至于策略模式建议,你可以通常建立你的过滤器块使用复合模式,然后传递这样构建的对象作为策略。

As for the Strategy Pattern suggested, you can usually build your filter by block using the Composite Pattern and then pass the object thus built as the strategy.

复合模式特别适合于表示解析表达式的结果,例如,想要看看表达式树以获得想法。

The Composite Pattern is especially suited to represent the result of a parsed expression for example, you might want to have a look at expressions trees to get an idea.

这篇关于过滤项目集合的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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