- 浏览: 39845 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
使用策略模式(Strategy)实现多关键字排序
“策略模式”的出现,是为了提供一套相互之间可灵活替换的算法,在不影响上层接口的情况下,用户可以自由选择不同的算法完成逻辑。
策略模式的UML示意图如下:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype>
该项目是一个小型的商业网站,逻辑层由Servlet实现。用户有一个需求,需要从数据库调出商品信息后能够对记录按名称(name)、规格(standard)、价格(price)和注册日期(register date)进行排序。虽然可以考虑按不同的排序关键字使用不同的sql语句(即不同的order by)查询数据库。但这样做的网络数据流量太大,每次需要从数据库完整地取回一个数据集(ResultSet),所以我选择了将数据集一次取到客户端的一个List中,然后在客户端按不同的关键字对List进行排序。
java.util.Collections类中有一个public static void sort(List list, Comparator comparator)的方法,可以按照不同的Comparator对象对list进行排序,它使用的是快速排序,所以效率非常高。而java.util.Comparator是一个接口:











其中和排序有关的是compare方法,它返回一个整数,若第一个参数比第二个参数“大”,则返回一个正数,若“小”则返回一个负数,若“相等”则返回0,这里的“大”、“小”、“相等”是由compare方法具体实现定义的一种比较标准。由这个方法不禁想到C语言中qsort函数中使用的函数指针int *compare(*,*),JAVA中之所以没有函数指针,就是用接口取代了(更详细的介绍见《Effective Java》中第22条“用类和接口来代替函数指针”)。
很明显,Comparator接口就是我们的“抽象策略”,sort方法就是我们的“消费API”,而不同的“具象策略”就是我们从Comparator接口实现的根据不同关键字排序的类。
整个排序模型的UML图如下,其中为了作图方便,我从Comparator继承了一个接口ItemComparator,根据不同的关键字从ItemComparator接口泛化了NameComparator类,PriceComparator类,RegisterDateComparator类和StandardComparator类。实际运用中不需要这个接口,四个类可以直接从Comparator泛化。ItemBean是一个封装了商品信息的bean,List中存放的就是这些bean。ItemSort和ItemComparator以及四个具体类实现了“简单工厂”模式,并封装了sort方法。
下面是具体的代码(
NameComparator类:



















PriceComparator类


















RegisterDateComparator类




















ItemSort类






















TestItemSort类






















发表评论
-
在Java中使用Oracle blob
2005-02-13 22:27 789Oracle中的lob (Large Object)可以存储非 ... -
java.util.StringTokenization
2005-02-13 22:29 649今天室友老七做数据库大作业,其中要实现一个功能,就是输入一个以 ... -
JBuilder 2005中的Servlet mapping
2005-02-13 22:30 707使用JB进行Servlet好像很方便的样子,但是servlet ... -
static与final变量
2005-08-04 12:27 759一、illegal forward refrence 前天写一 ... -
Alloy
2005-08-04 13:09 671Alloy look and feel(http:// ... -
IDEA
2005-08-04 13:11 929今天在TSS上看到IntelliJ的IDEA 5.0终于rel ... -
原型模式(Prototype)
2005-08-05 10:54 749一、概述 原型模式属于对象创建模式,通过给出一个原型对象来指明 ... -
IDEA破解过程
2005-08-05 19:11 1825开场白就不说了,直接切入正题吧。本破解的前提是安装了IDEA ... -
适配器模式(Adapter)
2005-08-06 19:39 629一、概述 类的适配器模式把被适配的类的API转换成为目标类的A ... -
A beginners guide to Dependency Injection [转载自TSS]
2005-08-08 22:30 826Scope This article presents a h ... -
Alloy破解过程
2005-08-09 15:42 8841package com.incors.plaf. ... -
接口与Object类的关系?
2005-08-19 13:53 1123今天凌晨coding的时候发现一个很有趣的现象。“Object ... -
缺省适配器模式(Default Adapter)
2005-09-06 12:01 788一、概述 当不需要全部实现适配器接口提供的方法时,可先设计一个 ... -
Object Modeling Strategies (I)
2005-09-08 10:12 600Activities and model components ... -
Joshua Bloch咏Tiger诗八首
2005-09-08 16:59 846“Ease of Development”是J2SE 1.5的 ... -
James Gosling
2005-09-14 22:01 779今天是Java China 2005的最后一天,下午没有去各个 ... -
Object Modeling Strategies (II)
2005-09-19 15:23 631Str#1d. "Invest an Hour&qu ... -
Object Modeling Strategies (III)
2005-09-19 15:26 575Identifying system purpose and ... -
Tomcat 5.5.9 不支持switch(<enum>)?
2005-09-19 18:51 825ServiceExceptionType: package p ... -
“软件危机”时总结的坏的编程习惯——我们是否依旧守着古风?
2005-10-09 14:23 7481、乱用GOTO语句(上下跳转看起来非常方便) 2、大量使用 ...
相关推荐
5. **策略模式(Strategy)** 策略模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换。在iOS中,可以用于处理不同的动画效果、排序算法或者支付方式等。 6. **装饰器模式(Decorator)** 装饰器...
C#中的策略模式常用于实现算法的动态选择,例如排序策略。 8. **适配器模式(Adapter)**:将一个类的接口转换成客户希望的另一个接口。适配器使原本因接口不兼容而无法一起工作的类可以协同工作。在C#中,可以通过...
6. **策略模式**:定义一系列算法,并将每个算法封装起来,使它们可以相互替换。Swift中,可以使用函数或闭包作为策略。 ```swift protocol SortingStrategy { func sort(items: [Int]) -> [Int] } func ...
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 13、&和&&的区别。 &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)。 14、...
- **策略模式(Strategy)**:定义了一系列算法,并将每一个算法封装起来,使它们可以相互替换。 - **模板方法模式(Template Method)**:定义一个操作中的算法骨架,而将一些步骤延迟到子类中。 - **职责链模式...
- **Strategy接口**:策略模式接口在实现特定功能时的使用方法。 - **线性表的顺序存储与实现** - 讲解了顺序存储结构的原理和实现细节,以及如何在Java中实现这种存储方式。 - **线性表的链式存储与实现** - ...
- Strategy模式是一种设计模式,允许算法的变化独立于使用它的客户端。 - 在处理线性表时,可以使用不同的策略来实现特定功能。 ##### 3.2 线性表的顺序存储与实现 - **顺序存储** - 使用连续的内存空间存储...
template模板方法模式、memento备忘录模式、observer观察者模式、command命令模式、state状态模式、strategy策略模式、mediator调停者模式、interpreter解释器模式、visitor访问者模式、chain of responsibility责任...
5. **设计模式**: 源码中可能会采用一些常见的设计模式,如单例模式(Singleton)用于管理全局资源,工厂模式(Factory)用于对象的创建,策略模式(Strategy)用于灵活地选择算法,以及观察者模式(Observer)用于...
- **Strategy接口**:可能是介绍策略模式在实现线性表中的应用。 - **线性表的顺序存储与实现** - 讲解了顺序表的存储特点,以及顺序表的增删改查等操作的具体实现方法。 - **线性表的链式存储与实现** - **...
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 10、&和&&的区别。 &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)。 11、HashMap...
- **策略模式**:定义了一个算法族,分别封装起来,使它们可以互相替换。 ##### 3.2 线性表的顺序存储与实现 - **顺序存储**:利用数组来存储线性表的数据。 - **操作特点**:查找速度快,但插入和删除操作较慢。 ...
- **Strategy接口**:讨论了策略模式(strategy pattern)在处理线性表时的应用。 - **线性表的顺序存储与实现** - 介绍了一种使用数组作为底层存储结构来实现线性表的方法,包括插入、删除等操作的具体实现。 - **...
6. **策略模式(Strategy)**:定义一系列的算法,把它们一个个封装起来,并且使它们可以互相替换。Java8的函数式接口如`Runnable`、`Comparator`等可以作为策略的载体。 7. **函数式编程模式**:Java8引入的Lambda...
- **Strategy接口**:讨论策略模式(strategy pattern)在处理线性表操作时的应用。 - **线性表的顺序存储与实现** - **顺序存储**:介绍顺序表的存储方式,即使用数组实现线性表。 - **实现**:展示顺序表的增删...
- **Strategy接口**:解释策略模式的概念,并讨论其在Java编程中的应用。 ##### 3.2 线性表的顺序存储与实现 - 讨论顺序存储的原理、优点(如访问速度快)和缺点(如插入删除操作效率低)。 ##### 3.3 线性表的...
- 用于定义策略模式中的接口。 - 实现不同策略以完成相同的操作。 **3.2 线性表的顺序存储与实现** - **顺序存储** - 将线性表中的元素存放在一片连续的内存空间中。 - 支持随机访问。 - **实现** - 初始化...
行为型模式如策略(Strategy)、观察者(Observer)和模板方法(Template Method)模式,它们关注对象之间的交互和职责分配,有助于实现更灵活的代码结构。 编码面试中,Java基础是非常关键的一环。这包括对数据...