经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
Java设计模式---Strategy策略模式
来源:cnblogs  作者:DingYu  时间:2018/12/3 10:09:08  对本文有异议

参考于 :

  大话设计模式

  马士兵设计模式视频

  

 

 

1.场景介绍

  购物网站上有一个产品,有三个字段,档次,价格,重量。

  有些同学喜欢轻的,有些手头紧,想要便宜的,有些喜欢档次高的。

  那么我们为了提高网站用户体验,必须给六个按钮,按照价格升序降序,按照档次升序降序,按照重量升序降序。

 (这里只是打个比方,好像一般遇到这种情况是用Lucenc)

2.不用策略模式

  1. package com.dingyu;
  2. import java.util.Arrays;
  3. /**
  4. * 产品类,这里为了代码代码尽可能的少,set get方法没加
  5. * Comparable接口中有一个compareTo方法,这个方法进行两个对象比较
  6. * Arrays.sort内部方法用到了这个compareTo方法
  7. * 这样以后只要我的类实现了Comparable接口,排序的代码可以通用(Arrays.sort())去排序,可以自定义比较规则,
  8. *
  9. * @author dingyu
  10. *
  11. */
  12. public class Product implements Comparable<Product> {
  13. private double quality;
  14. private double price;
  15. private int weight;
  16. public Product(double quality, double price, int weight) {
  17. this.quality = quality;
  18. this.price = price;
  19. this.weight = weight;
  20. }
  21. @Override
  22. public int compareTo(Product product) {
  23. if (this.price > product.price)
  24. return 1;
  25. else if (this.price == product.price)
  26. return 0;
  27. else
  28. return -1;
  29. }
  30. @Override
  31. public String toString() {
  32. return "价格 " + price + " 重量:" + weight + " 档次:" + quality;
  33. }
  34. public static void main(String[] args) {
  35. Product[] people = { new Product(2, 500, 50), new Product(3, 1000, 60), new Product(1, 200, 70) };
  36. Arrays.sort(people);
  37. for (int i = 0; i < people.length; i++) {
  38. System.out.println(people[i]);
  39. }
  40. }
  41. }

  JDK 源码:

   缺点:把compareTo逻辑写死了,如果要改,需要修改compareTo里的逻辑。违反开闭原则。

3.使用策略模式

  使用一个接口,每个策略都实现这个接口

  1. package com.dingyu;
  2. public interface Comparator<T> {
  3. public int compare(T t1,T t2);
  4. }
  1. package com.dingyu;
  2. public class CompareHeight implements Comparator<Product> {
  3. @Override
  4. public int compare(Product t1, Product t2) {
  5. if (t1.getPrice() > t2.getPrice())
  6. return 1;
  7. else if (t1.getPrice() == t2.getPrice())
  8. return 0;
  9. else
  10. return -1;
  11. }
  12. }
  1. package com.dingyu;
  2. public class CompareQunatity implements Comparator<Product>{
  3. @Override
  4. public int compare(Product t1, Product t2) {
  5. if (t1.getQuality() > t2.getQuality())
  6. return 1;
  7. else if (t1.getQuality() == t2.getQuality())
  8. return 0;
  9. else
  10. return -1;
  11. }
  12. }
  1. package com.dingyu;
  2. public class CompareWeight implements Comparator<Product> {
  3. @Override
  4. public int compare(Product t1, Product t2) {
  5. if (t1.getWeight() > t2.getWeight())
  6. return 1;
  7. else if (t1.getWeight() == t2.getWeight())
  8. return 0;
  9. else
  10. return -1;
  11. }
  12. }
  1. package com.dingyu;
  2. import java.util.Arrays;
  3. /**
  4. * 产品类,这里为了代码代码尽可能的少,set get方法没加 Comparable接口中有一个compareTo方法,这个方法进行两个对象比较
  5. * Arrays.sort内部方法用到了这个compareTo方法
  6. * 这样以后只要我的类实现了Comparable接口,排序的代码可以通用(Arrays.sort())去排序,可以自定义比较规则,
  7. *
  8. * @author dingyu
  9. *
  10. */
  11. public class Product implements Comparable<Product> {
  12. private double quality;
  13. private double price;
  14. private int weight;
  15. private static Comparator<Product> comparator;
  16. public Product(double quality, double price, int weight) {
  17. this.quality = quality;
  18. this.price = price;
  19. this.weight = weight;
  20. }
  21. public double getQuality() {
  22. return quality;
  23. }
  24. public void setQuality(double quality) {
  25. this.quality = quality;
  26. }
  27. public double getPrice() {
  28. return price;
  29. }
  30. public void setPrice(double price) {
  31. this.price = price;
  32. }
  33. public int getWeight() {
  34. return weight;
  35. }
  36. public void setWeight(int weight) {
  37. this.weight = weight;
  38. }
  39. public static Comparator<Product> getComparator() {
  40. return comparator;
  41. }
  42. public static void setComparator(Comparator<Product> comparator) {
  43. Product.comparator = comparator;
  44. }
  45. @Override
  46. public int compareTo(Product product) {
  47. return comparator.compare(this, product);
  48. }
  49. @Override
  50. public String toString() {
  51. return "价格 " + price + " 重量:" + weight + " 档次:" + quality;
  52. }
  53. public static void main(String[] args) {
  54. Product[] products = { new Product(2, 500, 50), new Product(3, 1000, 60), new Product(1, 200, 70) };
  55. Product.setComparator(new CompareHeight());
  56. Arrays.sort(products);
  57. for (int i = 0; i < people.length; i++) {
  58. System.out.println(people[i]);
  59. }
  60. }
  61. }

 

  

    

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号