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]);
}
}
}