From 34a50f0e2c5fa5794756463b526a9bd2b0f9cb7a Mon Sep 17 00:00:00 2001 From: Hcsp Bot Date: Wed, 18 Mar 2020 11:45:08 +0000 Subject: [PATCH] Revert "Refactor PriceCalculator with strategy pattern (#138)" This reverts commit 04f48d7dada3e8a61b7a9990547e1d4986aac4fc. --- .../hcsp/polymorphism/Discount95Strategy.java | 7 +--- .../hcsp/polymorphism/NoDiscountStrategy.java | 7 +--- .../polymorphism/OnlyVipDiscountStrategy.java | 11 +----- .../hcsp/polymorphism/PriceCalculator.java | 35 +++++++++---------- 4 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java b/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java index e336ecf..b6ae546 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java @@ -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 {} diff --git a/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java b/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java index 0bd73a6..bf4c18b 100644 --- a/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java @@ -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 {} diff --git a/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java b/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java index 65b1361..1714b1e 100644 --- a/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java @@ -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 {} diff --git a/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java b/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java index c4a3207..595fe5d 100644 --- a/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java +++ b/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java @@ -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!"); + } } }