参考于 :

  大话设计模式

  马士兵设计模式视频

  

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策略模式的更多相关文章

  1. Java的设计模式----strategy(策略模式)

    设计模式: 一个程序员对设计模式的理解: “不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开 ...

  2. 一天一个设计模式——Strategy策略模式

    一.模式说明 策略模式比较好理解,就是将程序中用到的算法整体的拿出来,并有多个不同版本的算法实现,在程序运行阶段,动态的决定使用哪个算法来解决问题. 举个实际的例子:排序算法的问题,假如我们的程序中需 ...

  3. C++设计模式-Strategy策略模式

    Strategy策略模式作用:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML图: Strategy模式将逻辑(算法)封装到一个类(Cont ...

  4. Java设计模式1——策略模式(Strategy Pattern)

    最近觅得一本好书<您的设计模式>,读完两章后就能断言,一定是一头极品屌丝写的,而且是专写给开发屌丝男的智慧枕边书,小女子就委屈一下,勉强看看,人笨,谁让他写得这么通俗易懂呢!为了加深理解, ...

  5. JAVA设计模式--strategy(策略者模式)

    概念策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The Strategy Pattern defines a fa ...

  6. JAVA设计模式一策略模式(Strategy Pattern)

    什么是设计模式? 就是一些经验.让程序代码更具弹性.好维护.代码复用的经验.而且设计模式都遵从一些OO设计原则. 题外话:以下罗列出常用的OO设计原则:链接 本文章介绍策略模式(Strategy Pa ...

  7. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  8. Java设计模式之策略模式(Strategy Pattern)

    简介 策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 组成 1.抽象策略角色(Strategy): 策略类,通常由一个接口或者抽象类实现. 2.具 ...

  9. Java设计模式之策略模式(Strategy)

    前言: 最近一直在学习基于okHttp网络请求,学习的过程中就想起了之前项目中有这么一个需求不同的接口要采用不同的加密方式,比如登录之前要采用RSA加密,登录之后要采用AES加密,当时是采用靠传递一个 ...

随机推荐

  1. 使用Elasticsearch的动态索引和索引优化

    关于映射 实际工作中使用过ES的朋友可能会有和静儿一样的感受.ES存储更新从编码上是很方便.如下,Kubernetes的yaml文件完全可以通过json直接序列化一下,一行代码存入ES. 剩下的工作可 ...

  2. Unity_新手必懂知识点

    翻车了!!!一个小例子带你了解闭包.事故现场:场景:6个button,上方1个text.点击button,text会显示button上的数字.代码如下: //在unity里面赋值public List ...

  3. 通过 bsondump 命令工具 解析备份产生的bson文件

    bsondump命令是将BSON格式的文件转换为可读性更强的文件格式,例如转为为JSON 格式的文档,bsondump默认转换为json格式的文档. 当通过mongodump命令进行备份时,如果有参数 ...

  4. ubuntu安装mysql没有让我设置密码

    终端输入: sudo cat /etc/mysql/debian.cnf显示内容:# Automatically generated for Debian scripts. DO NOT TOUCH! ...

  5. SQL Server死锁中的会话隔离级别为序列化(Serializable)实验测试

    最近在分析SQL Server的死锁时,发现一个比较有意思的现象,发现死锁当中一个会话的隔离级别为序列化(Serializable),这个是让人比较奇怪的地方,我们知道SQL Server数据库的默认 ...

  6. ioremap_nocache() 函数的使用【转】

    本篇文章主要是在ioremap_nocache函数说明的基础上进行整理,加入该函数的用法简介. 函数原型 void __iomem * ioremap_nocache (unsigned long o ...

  7. Video/Audio禁止快进(退)

    首先接着上个随笔.上个随笔主要介绍了视频音频的相关操作.属性和方法.这里主要记录一个应用:禁止快进(快退同理). 思路:监听快进事件(此处是监听播放时间更新),利用一个缓存的时间和播放到的时间进行对比 ...

  8. 有人WIFI模块使用详解

    补充 模块在连接路由器时如果希望模块固定IP 不过发现固定IP之后好像连接路由器的等待时间增加了 用的这一款 看一下现在可能用到了引脚 这个模块也有三种模式AP,STA,AP+STA 先说一下模块在A ...

  9. 在 ASP.NET Core 中集成 Skywalking APM

    前言 大家好,今天给大家介绍一下如何在 ASP.NET Core 项目中集成 Skywalking,Skywalking 是 Apache 基金会下面的一个开源 APM 项目,有些同学可能会 APM ...

  10. 广州 office365的开发者训练营交流活动简报

    2018年10月13日,在 微软广州办公室(广州市天河区太古汇1座28层微软广州办公室) 成功举办了office365的开发者训练营,本活动在微软官网的地址: https://www.microsof ...