Java设计模式---Strategy策略模式
参考于 :
大话设计模式
马士兵设计模式视频


1.场景介绍
购物网站上有一个产品,有三个字段,档次,价格,重量。
有些同学喜欢轻的,有些手头紧,想要便宜的,有些喜欢档次高的。
那么我们为了提高网站用户体验,必须给六个按钮,按照价格升序降序,按照档次升序降序,按照重量升序降序。
(这里只是打个比方,好像一般遇到这种情况是用Lucenc)
2.不用策略模式
package com.dingyu; import java.util.Arrays; /**
* 产品类,这里为了代码代码尽可能的少,set get方法没加
* Comparable接口中有一个compareTo方法,这个方法进行两个对象比较
* Arrays.sort内部方法用到了这个compareTo方法
* 这样以后只要我的类实现了Comparable接口,排序的代码可以通用(Arrays.sort())去排序,可以自定义比较规则,
*
* @author dingyu
*
*/
public class Product implements Comparable<Product> {
private double quality;
private double price;
private int weight; public Product(double quality, double price, int weight) {
this.quality = quality;
this.price = price;
this.weight = weight;
} @Override
public int compareTo(Product product) {
if (this.price > product.price)
return 1;
else if (this.price == product.price)
return 0;
else
return -1;
} @Override
public String toString() {
return "价格 " + price + " 重量:" + weight + " 档次:" + quality;
} public static void main(String[] args) {
Product[] people = { new Product(2, 500, 50), new Product(3, 1000, 60), new Product(1, 200, 70) };
Arrays.sort(people);
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
}
}
JDK 源码:

缺点:把compareTo逻辑写死了,如果要改,需要修改compareTo里的逻辑。违反开闭原则。
3.使用策略模式
使用一个接口,每个策略都实现这个接口
package com.dingyu;
public interface Comparator<T> {
public int compare(T t1,T t2);
}
package com.dingyu;
public class CompareHeight implements Comparator<Product> {
@Override
public int compare(Product t1, Product t2) {
if (t1.getPrice() > t2.getPrice())
return 1;
else if (t1.getPrice() == t2.getPrice())
return 0;
else
return -1;
}
}
package com.dingyu;
public class CompareQunatity implements Comparator<Product>{
@Override
public int compare(Product t1, Product t2) {
if (t1.getQuality() > t2.getQuality())
return 1;
else if (t1.getQuality() == t2.getQuality())
return 0;
else
return -1;
}
}
package com.dingyu;
public class CompareWeight implements Comparator<Product> {
@Override
public int compare(Product t1, Product t2) {
if (t1.getWeight() > t2.getWeight())
return 1;
else if (t1.getWeight() == t2.getWeight())
return 0;
else
return -1;
}
}
package com.dingyu; import java.util.Arrays; /**
* 产品类,这里为了代码代码尽可能的少,set get方法没加 Comparable接口中有一个compareTo方法,这个方法进行两个对象比较
* Arrays.sort内部方法用到了这个compareTo方法
* 这样以后只要我的类实现了Comparable接口,排序的代码可以通用(Arrays.sort())去排序,可以自定义比较规则,
*
* @author dingyu
*
*/
public class Product implements Comparable<Product> {
private double quality;
private double price;
private int weight;
private static Comparator<Product> comparator; public Product(double quality, double price, int weight) {
this.quality = quality;
this.price = price;
this.weight = weight;
} public double getQuality() {
return quality;
} public void setQuality(double quality) {
this.quality = quality;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public int getWeight() {
return weight;
} public void setWeight(int weight) {
this.weight = weight;
} public static Comparator<Product> getComparator() {
return comparator;
} public static void setComparator(Comparator<Product> comparator) {
Product.comparator = comparator;
} @Override
public int compareTo(Product product) {
return comparator.compare(this, product);
} @Override
public String toString() {
return "价格 " + price + " 重量:" + weight + " 档次:" + quality;
} public static void main(String[] args) {
Product[] products = { new Product(2, 500, 50), new Product(3, 1000, 60), new Product(1, 200, 70) };
Product.setComparator(new CompareHeight());
Arrays.sort(products);
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
}
}
Java设计模式---Strategy策略模式的更多相关文章
- Java的设计模式----strategy(策略模式)
设计模式: 一个程序员对设计模式的理解: “不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开 ...
- 一天一个设计模式——Strategy策略模式
一.模式说明 策略模式比较好理解,就是将程序中用到的算法整体的拿出来,并有多个不同版本的算法实现,在程序运行阶段,动态的决定使用哪个算法来解决问题. 举个实际的例子:排序算法的问题,假如我们的程序中需 ...
- C++设计模式-Strategy策略模式
Strategy策略模式作用:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML图: Strategy模式将逻辑(算法)封装到一个类(Cont ...
- Java设计模式1——策略模式(Strategy Pattern)
最近觅得一本好书<您的设计模式>,读完两章后就能断言,一定是一头极品屌丝写的,而且是专写给开发屌丝男的智慧枕边书,小女子就委屈一下,勉强看看,人笨,谁让他写得这么通俗易懂呢!为了加深理解, ...
- JAVA设计模式--strategy(策略者模式)
概念策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The Strategy Pattern defines a fa ...
- JAVA设计模式一策略模式(Strategy Pattern)
什么是设计模式? 就是一些经验.让程序代码更具弹性.好维护.代码复用的经验.而且设计模式都遵从一些OO设计原则. 题外话:以下罗列出常用的OO设计原则:链接 本文章介绍策略模式(Strategy Pa ...
- JAVA设计模式之策略模式 - Strategy
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...
- Java设计模式之策略模式(Strategy Pattern)
简介 策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 组成 1.抽象策略角色(Strategy): 策略类,通常由一个接口或者抽象类实现. 2.具 ...
- Java设计模式之策略模式(Strategy)
前言: 最近一直在学习基于okHttp网络请求,学习的过程中就想起了之前项目中有这么一个需求不同的接口要采用不同的加密方式,比如登录之前要采用RSA加密,登录之后要采用AES加密,当时是采用靠传递一个 ...
随机推荐
- webpack Code Splitting浅析
Code Splitting是webpack的一个重要特性,他允许你将代码打包生成多个bundle.对多页应用来说,它是必须的,因为必须要配置多个入口生成多个bundle:对于单页应用来说,如果只打包 ...
- 带你学习AOP框架之Aspect.Core[1]
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...
- asp.net core系列 45 Web应用 模型绑定和验证
一. 模型绑定 ASP.NET Core MVC 中的模型绑定,是将 HTTP 请求中的数据映射到action方法参数. 这些参数可能是简单类型的参数,如字符串.整数或浮点数,也可能是复杂类型的参数. ...
- 前端笔记之NodeJS(二)路由&REPL&模块系统&npm
一.路由机制(静态资源文件处理) 1.1 Nodejs没有根目录 MIME类型:http://www.w3school.com.cn/media/media_mimeref.asp 在Apache中, ...
- 使用Git过程中经常会遇到的问题
目录 git pull如何强制覆盖本地文件 Git如何同时删除本地分支和远程分支 Git如何撤销最近一次提交 Git撤销本地的最后一次提交 Git撤销最近一次远程提交 如何修改提交信息和文件 修改本地 ...
- HTML 练习绑定onclick事件
方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- SQL Server 数据库部分常用语句小结(三)
21.SQL运行Log的读取 .EXEC xp_readerrorlog 0,1,null,null,'开始时间','结束时间' 22. Alwayson 状况及传输情况监控 SELECT ar.re ...
- netdom remove 错误:netdom remove
自己用错了命令,直接将加入域的计算机使用dsrm删除了,本来应该使用netdom remove的,结果在域控制器上使用netdom remove错误,在客户端上登录时一样提示:netdom remov ...
- 函数声明 和 var声明的优先级
function demo() { console.log(5) } var demo = function(){ console.log(4) } console.log(demo()) var d ...
- springboot 多线程执行
一.springboot开线程执行异步任务 1.Spring通过任务执行器TaskExecutor,来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线程池的Tas ...