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加密,当时是采用靠传递一个 ...
随机推荐
- [翻译 EF Core in Action 2.3] 理解EF Core数据库查询
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- Emmagee--APP性能测试工具的基本使用
一.Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而 ...
- Linux学习_012_Centos 6.8 安装 Netcat
测试中,需要通过 Netcat 发送数据. 配置环境:CentOS 6.8 1.下载安装包到指定目录,例如本博主的是:/opt/software/ wget https://sourceforge.n ...
- asp.net core系列 53 IdentityServer4 (IS4)介绍
一.概述 在物理层之间相互通信必须保护资源,需要实现身份验证和授权,通常针对同一个用户存储.对于资源安全设计包括二个部分,一个是认证,一个是API访问. 1 认证 认证是指:应用程序需要知道当前用户的 ...
- DownEditTextView【自定义Edittext对Android 软键盘向下的监听】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录自定义EditText控件实现监听软键盘隐藏事件的功能.基本上和参考资料相同. 效果图 代码分析 自定义EditText子 ...
- 为什么range不是迭代器?range到底是什么类型?
迭代器是 23 种设计模式中最常用的一种(之一),在 Python 中随处可见它的身影,我们经常用到它,但是却不一定意识到它的存在.在关于迭代器的系列文章中(链接见文末),我至少提到了 23 种生成迭 ...
- .NETCore 新型 ORM 功能介绍
简介 FreeSql 是一个功能强大的 .NETStandard 库,用于对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.6.1+. 定义 IFre ...
- jQuery --- 第四期 (jQuery动效)
学习笔记 1.jQuery动画的淡入淡出 <!doctype html> <html> <head> <meta charset="utf-8&qu ...
- openlayers4 入门开发系列之地图导航控件篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- git rebase 合并多次提交.
一.应用场景 为什么需要合并多个提交呢? 常常一个功能的开发,修修补补 commit 了 n 多次,带来的结果就是提交过多过杂,不够直观,究竟哪些提交是对应这个功能的呢?还有就是,如果我要将这个功能迁 ...