diff --git a/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java b/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java index b6ae546..89fc1c6 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/Discount95Strategy.java @@ -1,3 +1,8 @@ package com.github.hcsp.polymorphism; -public class Discount95Strategy {} +public class Discount95Strategy extends PriceCalculator { + @Override + public int discount(int price,User user){ + return (int) (price*0.95); + } +} diff --git a/src/main/java/com/github/hcsp/polymorphism/DiscountStrategy.java b/src/main/java/com/github/hcsp/polymorphism/DiscountStrategy.java index 66fbd92..f7c5bb6 100644 --- a/src/main/java/com/github/hcsp/polymorphism/DiscountStrategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/DiscountStrategy.java @@ -1,6 +1,6 @@ package com.github.hcsp.polymorphism; -public class DiscountStrategy { +public class DiscountStrategy extends PriceCalculator { public int discount(int price, User user) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java b/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java index bf4c18b..d3a0a61 100644 --- a/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/NoDiscountStrategy.java @@ -1,3 +1,9 @@ package com.github.hcsp.polymorphism; -public class NoDiscountStrategy {} +public class NoDiscountStrategy extends PriceCalculator { + @Override + public int discount(int price, User user) { + return price; + } + +} diff --git a/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java b/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java index 1714b1e..e1c2a7c 100644 --- a/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java +++ b/src/main/java/com/github/hcsp/polymorphism/OnlyVipDiscountStrategy.java @@ -1,3 +1,13 @@ package com.github.hcsp.polymorphism; -public class OnlyVipDiscountStrategy {} +public class OnlyVipDiscountStrategy extends PriceCalculator { + @Override + public int discount(int price,User user){ + if(user.isVip()){ + return (int) (price*0.95); + }else{ + return price; + } + } + +} diff --git a/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java b/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java index 595fe5d..0f5139a 100644 --- a/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java +++ b/src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java @@ -1,28 +1,15 @@ package com.github.hcsp.polymorphism; -public class PriceCalculator { +public abstract class PriceCalculator { // 使用策略模式重构这个方法,实现三个策略: // NoDiscountStrategy 不打折 // Discount95Strategy 全场95折 // OnlyVipDiscountStrategy 只有VIP打95折,其他人保持原价 // 重构后的方法签名: // public static int calculatePrice(DiscountStrategy strategy, int price, User user) - 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!"); - } + public int calculatePrice(DiscountStrategy strategy, int price, User user) { + return strategy.discount(price,user); + } + }