-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用策略模式对打折系统进行重构 #214
base: master
Are you sure you want to change the base?
使用策略模式对打折系统进行重构 #214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package com.github.hcsp.polymorphism; | ||
|
||
public class Discount95Strategy {} | ||
public class Discount95Strategy extends DiscountStrategy{ | ||
@Override | ||
public int discount(int price, User user) { | ||
return (int)(price*0.95); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package com.github.hcsp.polymorphism; | ||
|
||
public class NoDiscountStrategy {} | ||
public class NoDiscountStrategy extends DiscountStrategy{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@Override | ||
public int discount(int price, User user) { | ||
return price; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
package com.github.hcsp.polymorphism; | ||
|
||
public class OnlyVipDiscountStrategy {} | ||
public class OnlyVipDiscountStrategy extends DiscountStrategy { | ||
@Override | ||
public int discount(int price, User user) { | ||
if (user.isVip()) { | ||
return (int) (price * 0.95); | ||
} else { | ||
return price; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,24 @@ public class PriceCalculator { | |
// 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 discountStrategy, int price, User user) { | ||
return discountStrategy.discount(price,user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// 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!"); | ||
// } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'{' 前应有空格。