在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java设计模式之(Strategy模式)

2016-7-29 15:47| 发布者: zhangjf| 查看: 682| 评论: 0

摘要: 基于有了OO的基础后,开始认真学习设计模式!设计模式是java设计中必不可少的! package strategy;/** * * @author Andy * */public class Apple implements Discountable { ...
基于有了OO的基础后,开始认真学习设计模式!设计模式是java设计中必不可少的!
  1. package strategy;
  2. /**
  3. *
  4. * @author Andy
  5. *
  6. */
  7. public class Apple implements Discountable {
  8. //重量
  9. private double weight;
  10. //单价 实际开发中 设计金钱等精确计算都是BigDecimal;
  11. private double price;
  12. //按购买量打折
  13. // private Discountor d = new AppleWeightDiscountor();
  14. //按购买总价打折
  15. private Discountor d = new ApplePriceDiscountor();
  16. public double getWeight() {
  17. return weight;
  18. }
  19. public void setWeight(double weight) {
  20. this.weight = weight;
  21. }
  22. public double getPrice() {
  23. return price;
  24. }
  25. public void setPrice(double price) {
  26. this.price = price;
  27. }
  28. public Apple (double weight,double price ){
  29. super();
  30. this.weight=weight;
  31. this.price=price;
  32. }
  33. @Override
  34. public void discountSell() {
  35. d.discount(this);
  36. }
  37. }
复制代码
  1. package strategy;
  2. /**
  3. *
  4. * @author Andy
  5. *
  6. */
  7. public class Banana implements Discountable {
  8. //重量
  9. private double weight;
  10. ////单价 实际开发中 涉及金钱等精确计算都是用BigDecimal
  11. private double price;
  12. public Banana(double weight, double price) {
  13. super();
  14. this.weight = weight;
  15. this.price = price;
  16. }
  17. public double getWeight() {
  18. return weight;
  19. }
  20. public void setWeight(double weight) {
  21. this.weight = weight;
  22. }
  23. public double getPrice() {
  24. return price;
  25. }
  26. public void setPrice(double price) {
  27. this.price = price;
  28. }
  29. @Override
  30. public void discountSell() {
  31. //打折算法
  32. if(weight < 5) {
  33. System.out.println("Banana未打折价钱: " + weight * price);
  34. }else if(weight >= 5 && weight < 10) {
  35. System.out.println("Banana打八八折价钱: " + weight * price * 0.88 );
  36. }else if(weight >= 10) {
  37. System.out.println("Banana打五折价钱: " + weight * price * 0.5 );
  38. }
  39. }
  40. }
复制代码
  1. package strategy;
  2. /**
  3. *
  4. * @author Andy
  5. *
  6. */
  7. public class Market {
  8. /**
  9. * 对可打折的一类事物进行打折
  10. * @param apple
  11. */
  12. public static void discountSell(Discountable d) {
  13. d.discountSell();
  14. }
  15. }
复制代码
  1. package strategy;
  2. /**
  3. *
  4. * @author Andy
  5. *
  6. */
  7. public interface Discountable {
  8. public void discountSell();
  9. }
复制代码
  1. package strategy;
  2. /**
  3. *
  4. * @author Andy
  5. *
  6. */
  7. public class Test {
  8. /**
  9. *
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13. // 只能对苹果打折 还不能对通用的一类事物打折 而且都是要卖什么就写什么打折算法
  14. // 其实每类事物打折算法又是不一致的
  15. Discountable d = new Apple(10.3, 3.6);
  16. Discountable d1= new Banana(5.4,1.1);
  17. Market.discountSell(d);
  18. Market.discountSell(d1);
  19. }
  20. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 13:43

Copyright 2015-2025 djqfx

返回顶部