Skip to content

Commit

Permalink
Revert "Refactor PriceCalculator with strategy pattern (#138)"
Browse files Browse the repository at this point in the history
This reverts commit 04f48d7.
  • Loading branch information
hcsp-bot committed Mar 18, 2020
1 parent 04f48d7 commit 34a50f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package com.github.hcsp.polymorphism;

public class Discount95Strategy extends DiscountStrategy {
@Override
public int discount(int price, User user) {
return (int)(price * 0.95);
}
}
public class Discount95Strategy {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package com.github.hcsp.polymorphism;

public class NoDiscountStrategy extends DiscountStrategy {
@Override
public int discount(int price, User user) {
return price;
}
}
public class NoDiscountStrategy {}
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
package com.github.hcsp.polymorphism;

public class OnlyVipDiscountStrategy extends DiscountStrategy {
@Override
public int discount(int price, User user) {
if (user.isVip()) {
return (int)(price * 0.95);
} else {
return price;
}
}
}
public class OnlyVipDiscountStrategy {}
35 changes: 17 additions & 18 deletions src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ public class PriceCalculator {
// OnlyVipDiscountStrategy 只有VIP打95折,其他人保持原价
// 重构后的方法签名:
// public static int calculatePrice(DiscountStrategy strategy, int price, User user)
public static int calculatePrice(DiscountStrategy discountStrategy, int price, User user) {
return discountStrategy.discount(price, user);
// switch (discountStrategy) {
// case "NoDiscount":
// return price;
// case "Discount95":
// return (int) (price * 0.95);
// case "OnlyVip":
// {
// if (user.isVip()) {
// return (int) (price * 0.95);
// } else {
// return price;
// }
// }
// default:
// throw new IllegalStateException("Should not be here!");
// }
public static int calculatePrice(String discountStrategy, int price, User user) {
switch (discountStrategy) {
case "NoDiscount":
return price;
case "Discount95":
return (int) (price * 0.95);
case "OnlyVip":
{
if (user.isVip()) {
return (int) (price * 0.95);
} else {
return price;
}
}
default:
throw new IllegalStateException("Should not be here!");
}
}
}

0 comments on commit 34a50f0

Please sign in to comment.